Skip to content

Commit c20c804

Browse files
committed
introducting 0http.21no.de
1 parent 3763349 commit c20c804

5 files changed

Lines changed: 254 additions & 168 deletions

File tree

README.md

Lines changed: 8 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# 0http
1+
# Introduction
22
[![NPM version](https://badgen.net/npm/v/0http)](https://www.npmjs.com/package/0http)
33
[![NPM Total Downloads](https://badgen.net/npm/dt/0http)](https://www.npmjs.com/package/0http)
44
[![License](https://badgen.net/npm/license/0http)](https://www.npmjs.com/package/0http)
55
[![TypeScript support](https://badgen.net/npm/types/0http)](https://www.npmjs.com/package/0http)
66
[![Github stars](https://badgen.net/github/stars/jkyberneees/0http?icon=github)](https://github.com/jkyberneees/0http)
77

88
Cero friction HTTP framework:
9-
- Tweaked Node.js Server for high throughput.
10-
- Use the request router you like.
9+
- Tweaked Node.js HTTP server for high throughput.
10+
- High-performance and customizable request routers.
1111

12-
![Performance Benchmarks](Benchmarks.png)
13-
> [You can read more about this numbers here.](https://github.com/the-benchmarker/web-frameworks/blob/e00f4b9fc3db7105d8c918c36691560be069697c/README.md)
12+
![Performance Benchmarks](docs/Benchmarks.png)
13+
> Check it yourself: https://web-frameworks-benchmark.netlify.app/result?f=feathersjs,0http,koa,fastify,nestjs-express,express,sails,nestjs-fastify,restana
1414
1515
## Usage
1616
```js
@@ -32,170 +32,10 @@ router.post('/do', (req, res) => {
3232
server.listen(3000)
3333
```
3434

35-
## Routers
36-
`0http` allows you to define the router implementation you prefer as soon as it support the following interface:
37-
```js
38-
router.lookup = (req, res) // -> should trigger router search and handlers execution
39-
```
40-
41-
### 0http - sequential (default router)
42-
This a `0http` extended implementation of the [trouter](https://www.npmjs.com/package/trouter) router. Includes support for middlewares, nested routers and shortcuts for routes registration.
43-
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
44-
an internal(optional) LRU cache to store the matching results of the previous requests, resulting on a super-fast matching process.
45-
46-
Supported HTTP verbs: `GET, HEAD, PATCH, OPTIONS, CONNECT, DELETE, TRACE, POST, PUT`
47-
48-
```js
49-
const cero = require('0http')
50-
const { router, server } = cero({})
51-
52-
// global middleware example
53-
router.use('/', (req, res, next) => {
54-
res.write('Hello ')
55-
next()
56-
})
57-
58-
// route middleware example
59-
const routeMiddleware = (req, res, next) => {
60-
res.write('World')
61-
next()
62-
}
63-
64-
// GET /sayhi route with middleware and handler
65-
router.get('/sayhi', routeMiddleware, (req, res) => {
66-
res.end('!')
67-
})
68-
69-
server.listen(3000)
70-
```
71-
#### Configuration Options
72-
- **defaultRoute**: Route handler when there is no router matching. Default value:
73-
```js
74-
(req, res) => {
75-
res.statusCode = 404
76-
res.end()
77-
}
78-
```
79-
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
80-
- **errorHandler**: Global error handler function. Default value:
81-
82-
```js
83-
(err, req, res) => {
84-
res.statusCode = 500
85-
res.end(err.message)
86-
}
87-
```
88-
89-
* **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
90-
91-
Example passing configuration options:
92-
93-
```js
94-
const sequential = require('0http/lib/router/sequential')
95-
const { router, server } = cero({
96-
router: sequential({
97-
cacheSize: 2000
98-
})
99-
})
100-
```
101-
102-
#### Async middlewares
103-
You can use async middlewares to await the remaining chain execution. Let's describe with a custom error handler middleware:
104-
```js
105-
router.use('/', async (req, res, next) => {
106-
try {
107-
await next()
108-
} catch (err) {
109-
res.statusCode = 500
110-
res.end(err.message)
111-
}
112-
})
113-
114-
router.get('/sayhi', (req, res) => {
115-
throw new Error('Uuuups!')
116-
})
117-
```
118-
119-
#### Nested Routers
120-
You can simply use `sequential` router intances as nested routers:
121-
```js
122-
const cero = require('../index')
123-
const { router, server } = cero({})
124-
125-
const nested = require('0http/lib/router/sequential')()
126-
nested.get('/url', (req, res, next) => {
127-
res.end(req.url)
128-
})
129-
router.use('/v1', nested)
130-
131-
server.listen(3000)
132-
```
133-
134-
### find-my-way router
135-
> https://github.com/delvedor/find-my-way
136-
137-
Super-fast raw HTTP router with no goodies. Internally uses a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree)
138-
router that will bring better performance over iterative regular expressions matching.
139-
```js
140-
const cero = require('../index')
141-
const { router, server } = cero({
142-
router: require('find-my-way')()
143-
})
144-
145-
router.on('GET', '/hi', (req, res) => {
146-
res.end('Hello World!')
147-
})
148-
149-
server.listen(3000)
150-
```
151-
152-
153-
## Servers
154-
`0http` is just a wrapper for the servers and routers implementations you provide.
155-
```js
156-
const cero = require('0http')
157-
158-
const { router, server } = cero({
159-
server: yourCustomServerInstance
160-
})
161-
```
162-
163-
### Node.js http.Server
164-
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.
165-
Because this server offers the best balance between Node.js ecosystem compatibility and performance, we highly recommend it for most use cases.
166-
167-
## Benchmarks (30/12/2019)
168-
**Node version**: v12.14.0
169-
**Laptop**: MacBook Pro 2019, 2,4 GHz Intel Core i9, 32 GB 2400 MHz DDR4
170-
**Server**: Single instance
171-
172-
```bash
173-
wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi
174-
```
175-
176-
### 1 route registered
177-
- 0http (sequential)
178-
`Requests/sec: 88438.69`
179-
- 0http (find-my-way)
180-
`Requests/sec: 87597.44`
181-
- restana v3.4.2
182-
`Requests/sec: 73455.97`
183-
184-
### 5 routes registered
185-
- **0http (sequential)**
186-
`Requests/sec: 85839.17`
187-
- 0http (find-my-way)
188-
`Requests/sec: 82682.86`
189-
190-
> For more accurate benchmarks please see:
191-
>
192-
> - https://github.com/the-benchmarker/web-frameworks
193-
194-
## Support / Donate 💚
35+
# Support / Donate 💚
19536
You can support the maintenance of this project:
19637
- PayPal: https://www.paypal.me/kyberneees
19738
- [TRON](https://www.binance.com/en/buy-TRON) Wallet: `TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus`
19839

199-
## Breaking Changes:
200-
### 3.x
201-
- Low HTTP server implementation was moved to: https://github.com/jkyberneees/low-http-server
40+
# More
41+
- Website and documentation: https://0http.21no.de

docs/.nojekyll

Whitespace-only changes.
File renamed without changes.

docs/README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Introduction
2+
[![NPM version](https://badgen.net/npm/v/0http)](https://www.npmjs.com/package/0http)
3+
[![NPM Total Downloads](https://badgen.net/npm/dt/0http)](https://www.npmjs.com/package/0http)
4+
[![License](https://badgen.net/npm/license/0http)](https://www.npmjs.com/package/0http)
5+
[![TypeScript support](https://badgen.net/npm/types/0http)](https://www.npmjs.com/package/0http)
6+
[![Github stars](https://badgen.net/github/stars/jkyberneees/0http?icon=github)](https://github.com/jkyberneees/0http)
7+
8+
Cero friction HTTP framework:
9+
- Tweaked Node.js HTTP server for high throughput.
10+
- High-performance and customizable request routers.
11+
12+
![Performance Benchmarks](Benchmarks.png)
13+
> Check it yourself: https://web-frameworks-benchmark.netlify.app/result?f=feathersjs,0http,koa,fastify,nestjs-express,express,sails,nestjs-fastify,restana
14+
15+
## Usage
16+
```js
17+
const cero = require('0http')
18+
const { router, server } = cero()
19+
20+
router.get('/hello', (req, res) => {
21+
res.end('Hello World!')
22+
})
23+
24+
router.post('/do', (req, res) => {
25+
// ...
26+
res.statusCode = 201
27+
res.end()
28+
})
29+
30+
//...
31+
32+
server.listen(3000)
33+
```
34+
35+
# Routers
36+
`0http` allows you to define the router implementation you prefer as soon as it support the following interface:
37+
```js
38+
router.lookup = (req, res) // -> should trigger router search and handlers execution
39+
```
40+
41+
## 0http - sequential (default router)
42+
This a `0http` extended implementation of the [trouter](https://www.npmjs.com/package/trouter) router. Includes support for middlewares, nested routers and shortcuts for routes registration.
43+
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
44+
an internal(optional) LRU cache to store the matching results of the previous requests, resulting on a super-fast matching process.
45+
46+
Supported HTTP verbs: `GET, HEAD, PATCH, OPTIONS, CONNECT, DELETE, TRACE, POST, PUT`
47+
48+
```js
49+
const cero = require('0http')
50+
const { router, server } = cero({})
51+
52+
// global middleware example
53+
router.use('/', (req, res, next) => {
54+
res.write('Hello ')
55+
next()
56+
})
57+
58+
// route middleware example
59+
const routeMiddleware = (req, res, next) => {
60+
res.write('World')
61+
next()
62+
}
63+
64+
// GET /sayhi route with middleware and handler
65+
router.get('/sayhi', routeMiddleware, (req, res) => {
66+
res.end('!')
67+
})
68+
69+
server.listen(3000)
70+
```
71+
### Configuration Options
72+
- **defaultRoute**: Route handler when there is no router matching. Default value:
73+
```js
74+
(req, res) => {
75+
res.statusCode = 404
76+
res.end()
77+
}
78+
```
79+
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
80+
- **errorHandler**: Global error handler function. Default value:
81+
82+
```js
83+
(err, req, res) => {
84+
res.statusCode = 500
85+
res.end(err.message)
86+
}
87+
```
88+
89+
* **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
90+
91+
Example passing configuration options:
92+
93+
```js
94+
const sequential = require('0http/lib/router/sequential')
95+
const { router, server } = cero({
96+
router: sequential({
97+
cacheSize: 2000
98+
})
99+
})
100+
```
101+
102+
### Async middlewares
103+
You can use async middlewares to await the remaining chain execution. Let's describe with a custom error handler middleware:
104+
```js
105+
router.use('/', async (req, res, next) => {
106+
try {
107+
await next()
108+
} catch (err) {
109+
res.statusCode = 500
110+
res.end(err.message)
111+
}
112+
})
113+
114+
router.get('/sayhi', (req, res) => {
115+
throw new Error('Uuuups!')
116+
})
117+
```
118+
119+
### Nested Routers
120+
You can simply use `sequential` router intances as nested routers:
121+
```js
122+
const cero = require('../index')
123+
const { router, server } = cero({})
124+
125+
const nested = require('0http/lib/router/sequential')()
126+
nested.get('/url', (req, res, next) => {
127+
res.end(req.url)
128+
})
129+
router.use('/v1', nested)
130+
131+
server.listen(3000)
132+
```
133+
134+
## find-my-way router
135+
> https://github.com/delvedor/find-my-way
136+
137+
Super-fast raw HTTP router with no goodies. Internally uses a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree)
138+
router that will bring better performance over iterative regular expressions matching.
139+
```js
140+
const cero = require('../index')
141+
const { router, server } = cero({
142+
router: require('find-my-way')()
143+
})
144+
145+
router.on('GET', '/hi', (req, res) => {
146+
res.end('Hello World!')
147+
})
148+
149+
server.listen(3000)
150+
```
151+
152+
# Servers
153+
`0http` is just a wrapper for the servers and routers implementations you provide.
154+
```js
155+
const cero = require('0http')
156+
157+
const { router, server } = cero({
158+
server: yourCustomServerInstance
159+
})
160+
```
161+
162+
## Node.js http.Server
163+
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.
164+
Because this server offers the best balance between Node.js ecosystem compatibility and performance, we highly recommend it for most use cases.
165+
166+
# Benchmarks (30/12/2019)
167+
**Node version**: v12.14.0
168+
**Laptop**: MacBook Pro 2019, 2,4 GHz Intel Core i9, 32 GB 2400 MHz DDR4
169+
**Server**: Single instance
170+
171+
```bash
172+
wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi
173+
```
174+
175+
## 1 route registered
176+
- 0http (sequential)
177+
`Requests/sec: 88438.69`
178+
- 0http (find-my-way)
179+
`Requests/sec: 87597.44`
180+
- restana v3.4.2
181+
`Requests/sec: 73455.97`
182+
183+
## 5 routes registered
184+
- **0http (sequential)**
185+
`Requests/sec: 85839.17`
186+
- 0http (find-my-way)
187+
`Requests/sec: 82682.86`
188+
189+
> For more accurate benchmarks please see:
190+
>
191+
> - https://github.com/the-benchmarker/web-frameworks
192+
193+
# Support / Donate 💚
194+
You can support the maintenance of this project:
195+
- PayPal: https://www.paypal.me/kyberneees
196+
- [TRON](https://www.binance.com/en/buy-TRON) Wallet: `TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus`
197+
198+
# Breaking Changes:
199+
## 3.x
200+
- Low HTTP server implementation was moved to: https://github.com/jkyberneees/low-http-server

0 commit comments

Comments
 (0)