0http 中文文档教程
0http
Cero 摩擦 HTTP 框架:
- Tweaked Node.js Server for high throughput.
- Use the request router you like.
Usage
const cero = require('0http')
const { router, server } = cero()
router.get('/hello', (req, res) => {
res.end('Hello World!')
})
router.post('/do', (req, res) => {
// ...
res.statusCode = 201
res.end()
})
//...
server.listen(3000)
Routers
0http
允许你定义你喜欢的路由器实现,只要它支持以下接口:
router.lookup = (req, res) // -> should trigger router search and handlers execution
0http - sequential (default router)
这是一个 0http
trouter 路由器的扩展实现。 包括对中间件、嵌套路由器和路由注册快捷方式的支持。
由于这是一个迭代的正则表达式匹配路由器,当注册路由数量增加时,它往往比 find-my-way
慢; 为了缓解这个问题,我们使用 内部(可选)LRU 缓存,用于存储先前请求的匹配结果,从而实现超快速匹配过程。
支持的 HTTP 动词:GET、HEAD、PATCH、OPTIONS、CONNECT、DELETE、TRACE、POST、PUT
const cero = require('0http')
const { router, server } = cero({})
// global middleware example
router.use('/', (req, res, next) => {
res.write('Hello ')
next()
})
// route middleware example
const routeMiddleware = (req, res, next) => {
res.write('World')
next()
}
// GET /sayhi route with middleware and handler
router.get('/sayhi', routeMiddleware, (req, res) => {
res.end('!')
})
server.listen(3000)
Configuration Options
- defaultRoute: Route handler when there is no router matching. Default value: ```js (req, res) => { res.statusCode = 404 res.end() }
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
- **errorHandler**: Global error handler function. Default value:
js (err, req, res) => { res.statusCode = 500 res.end(err.message) }
* **prioRequestsProcessing**: `true` to use SetImmediate to prioritize router lookup, `false` to disable. By default `true`, if used with native Node.js `http` and `https` servers. Set to `false`, if using Node.js Native Addon server, such as uWebSockets.js, as this will cause a huge performance penalty
Example passing configuration options:
_ const sequential = require('0http/lib/router/sequential') const { 路由器,服务器 } = cero({ 路由器:顺序({ 缓存大小:2000 }) })
#### Async middlewares
You can use async middlewares to await the remaining chain execution. Let's describe with a custom error handler middleware:
js router.use('/', async (req, res, next) => { 尝试 { 等待下一个() } 抓住(错误){ res.statusCode = 500 res.end(err.message) } })
router.get('/sayhi', (req, res) => { 抛出新错误('Uuuups!') })
#### Nested Routers
You can simply use `sequential` router intances as nested routers:
js const cero = require('../index') const { 路由器,服务器 } = cero({})
const nested = require('0http/lib/router/sequential')() nested.get('/url', (req, res, next) => { res.end(req.url)
}) router.use('/v1', 嵌套)
server.listen(3000)
### find-my-way router
> https://github.com/delvedor/find-my-way
Super-fast raw HTTP router with no goodies. Internally uses a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree)
router that will bring better performance over iterative regular expressions matching.
js const cero = require('../index') const { 路由器,服务器 } = cero({ 路由器:要求('find-my-way')() })
router.on('GET', '/hi', (req, res) => { res.end('你好,世界!') })
server.listen(3000)
## Servers
`0http` is just a wrapper for the servers and routers implementations you provide.
js const cero = require('0http')
const { 路由器,服务器 } = cero({ 服务器:您的自定义服务器实例 })
### Node.js http.Server
If no server is provided by configuration, the standard Node.js [http.Server](https://nodejs.org/api/http.html#http_class_http_server) implementation is used.
Because this server offers the best balance between Node.js ecosystem compatibility and performance, we highly recommend it for most use cases.
## Benchmarks (30/12/2019)
**Node version**: v12.14.0
**Laptop**: MacBook Pro 2019, 2,4 GHz Intel Core i9, 32 GB 2400 MHz DDR4
**Server**: Single instance
狂欢 wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi ```
1 route registered
- 0http (sequential)
Requests/sec: 88438.69
- 0http (find-my-way)
Requests/sec: 87597.44
- restana v3.4.2
Requests/sec: 73455.97
5 routes registered
- 0http (sequential)
Requests/sec: 85839.17
- 0http (find-my-way)
Requests/sec: 82682.86
有关更准确的基准,请参阅:
- https://github.com/the-benchmarker/web-frameworks
Support / Donate
0http
Cero friction HTTP framework:
- Tweaked Node.js Server for high throughput.
- Use the request router you like.
Usage
const cero = require('0http')
const { router, server } = cero()
router.get('/hello', (req, res) => {
res.end('Hello World!')
})
router.post('/do', (req, res) => {
// ...
res.statusCode = 201
res.end()
})
//...
server.listen(3000)
Routers
0http
allows you to define the router implementation you prefer as soon as it support the following interface:
router.lookup = (req, res) // -> should trigger router search and handlers execution
0http - sequential (default router)
This a 0http
extended implementation of the trouter router. Includes support for middlewares, nested routers and shortcuts for routes registration.
As this is an iterative regular expression matching router, it tends to be slower than find-my-way
when the number of registered routes increases; to mitigate this issue, we use an internal(optional) LRU cache to store the matching results of the previous requests, resulting on a super-fast matching process.
Supported HTTP verbs: GET, HEAD, PATCH, OPTIONS, CONNECT, DELETE, TRACE, POST, PUT
const cero = require('0http')
const { router, server } = cero({})
// global middleware example
router.use('/', (req, res, next) => {
res.write('Hello ')
next()
})
// route middleware example
const routeMiddleware = (req, res, next) => {
res.write('World')
next()
}
// GET /sayhi route with middleware and handler
router.get('/sayhi', routeMiddleware, (req, res) => {
res.end('!')
})
server.listen(3000)
Configuration Options
- defaultRoute: Route handler when there is no router matching. Default value: ```js (req, res) => { res.statusCode = 404 res.end() }
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
- **errorHandler**: Global error handler function. Default value:
js (err, req, res) => { res.statusCode = 500 res.end(err.message) }
* **prioRequestsProcessing**: `true` to use SetImmediate to prioritize router lookup, `false` to disable. By default `true`, if used with native Node.js `http` and `https` servers. Set to `false`, if using Node.js Native Addon server, such as uWebSockets.js, as this will cause a huge performance penalty
Example passing configuration options:
js const sequential = require('0http/lib/router/sequential') const { router, server } = cero({ router: sequential({ cacheSize: 2000 }) })
#### Async middlewares
You can use async middlewares to await the remaining chain execution. Let's describe with a custom error handler middleware:
js router.use('/', async (req, res, next) => { try { await next() } catch (err) { res.statusCode = 500 res.end(err.message) } })
router.get('/sayhi', (req, res) => { throw new Error('Uuuups!') })
#### Nested Routers
You can simply use `sequential` router intances as nested routers:
js const cero = require('../index') const { router, server } = cero({})
const nested = require('0http/lib/router/sequential')() nested.get('/url', (req, res, next) => { res.end(req.url)
}) router.use('/v1', nested)
server.listen(3000)
### find-my-way router
> https://github.com/delvedor/find-my-way
Super-fast raw HTTP router with no goodies. Internally uses a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree)
router that will bring better performance over iterative regular expressions matching.
js const cero = require('../index') const { router, server } = cero({ router: require('find-my-way')() })
router.on('GET', '/hi', (req, res) => { res.end('Hello World!') })
server.listen(3000)
## Servers
`0http` is just a wrapper for the servers and routers implementations you provide.
js const cero = require('0http')
const { router, server } = cero({ server: yourCustomServerInstance })
### Node.js http.Server
If no server is provided by configuration, the standard Node.js [http.Server](https://nodejs.org/api/http.html#http_class_http_server) implementation is used.
Because this server offers the best balance between Node.js ecosystem compatibility and performance, we highly recommend it for most use cases.
## Benchmarks (30/12/2019)
**Node version**: v12.14.0
**Laptop**: MacBook Pro 2019, 2,4 GHz Intel Core i9, 32 GB 2400 MHz DDR4
**Server**: Single instance
bash wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi ```
1 route registered
- 0http (sequential)
Requests/sec: 88438.69
- 0http (find-my-way)
Requests/sec: 87597.44
- restana v3.4.2
Requests/sec: 73455.97
5 routes registered
- 0http (sequential)
Requests/sec: 85839.17
- 0http (find-my-way)
Requests/sec: 82682.86
For more accurate benchmarks please see:
- https://github.com/the-benchmarker/web-frameworks