router-http 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -13
- package/package.json +2 -2
- package/src/index.js +4 -4
package/README.md
CHANGED
|
@@ -4,16 +4,15 @@
|
|
|
4
4
|
[](https://coveralls.io/github/Kikobeats/router-http)
|
|
5
5
|
[](https://www.npmjs.org/package/router-http)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
A middleware style router, similar to [express@router](https://github.com/pillarjs/router), plus:
|
|
8
8
|
|
|
9
|
-
-
|
|
9
|
+
- Faster (x3 compared with Express).
|
|
10
10
|
- Maintained and well tested.
|
|
11
|
-
- Smaller
|
|
12
|
-
- Most of router API is supported.
|
|
11
|
+
- Smaller (1.4 kB).
|
|
13
12
|
|
|
14
13
|
Don't get me wrong: The original Express router is a piece of art. I used it for years and I just considered create this library after experienced a bug that never was addressed in the stable version due to the [lack of maintenance](https://github.com/pillarjs/router/pull/60).
|
|
15
14
|
|
|
16
|
-
While I was evaluating the market for finding an alternative, I found [polka](https://github.com/lukeed/polka/tree/master/packages/polka) was a good starting point for creating a replacement. This
|
|
15
|
+
While I was evaluating the market for finding an alternative, I found [polka](https://github.com/lukeed/polka/tree/master/packages/polka) was a good starting point for creating a replacement. This library is different from polka in that it only contains the code that is strictly necessary for routing, nothing else.
|
|
17
16
|
|
|
18
17
|
## Install
|
|
19
18
|
|
|
@@ -30,17 +29,17 @@ const createRouter = require('router-http')
|
|
|
30
29
|
|
|
31
30
|
const router = createRouter((error, req, res) => {
|
|
32
31
|
const hasError = error !== undefined
|
|
33
|
-
res.statusCode = hasError ? 500 : 404
|
|
34
|
-
res.end(hasError ?
|
|
32
|
+
res.statusCode = hasError ? error.code ?? 500 : 404
|
|
33
|
+
res.end(hasError ? error.message ?? 'Internal Server Error' : 'Not Found')
|
|
35
34
|
})
|
|
36
35
|
```
|
|
37
36
|
|
|
38
37
|
The router requires a final handler that will be called if an error occurred or none of the routes match.
|
|
39
38
|
|
|
40
|
-
After that, you can declare any HTTP verb route:
|
|
41
|
-
|
|
42
39
|
### Declaring routes
|
|
43
40
|
|
|
41
|
+
The routes are declared using HTTP verbs:
|
|
42
|
+
|
|
44
43
|
```js
|
|
45
44
|
/**
|
|
46
45
|
* Declaring multiple routes based on the HTTP verb.
|
|
@@ -97,6 +96,18 @@ router
|
|
|
97
96
|
})
|
|
98
97
|
```
|
|
99
98
|
|
|
99
|
+
Also, you can declare conditional middlewares:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
/**
|
|
103
|
+
* Just add the middleware if it's production environment
|
|
104
|
+
*/
|
|
105
|
+
router
|
|
106
|
+
.use(process.env.NODE_ENV === 'production' && authentication())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
They are only will add if the condition is satisfied.
|
|
110
|
+
|
|
100
111
|
### Prefixing routes
|
|
101
112
|
|
|
102
113
|
In case you need you can prefix all the routes:
|
|
@@ -123,16 +134,39 @@ const server = http.createServer(router)
|
|
|
123
134
|
|
|
124
135
|
## Benchmark
|
|
125
136
|
|
|
126
|
-
|
|
137
|
+
**express@4.18.2**
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
Running 30s test @ http://localhost:3000/user/123
|
|
141
|
+
8 threads and 100 connections
|
|
142
|
+
Thread Stats Avg Stdev Max +/- Stdev
|
|
143
|
+
Latency 4.12ms 653.26us 21.71ms 89.35%
|
|
144
|
+
Req/Sec 2.93k 159.60 5.99k 84.75%
|
|
145
|
+
700421 requests in 30.06s, 102.87MB read
|
|
146
|
+
Requests/sec: 23304.22
|
|
147
|
+
Transfer/sec: 3.42MB
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**router-http@1.0.0**
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
Running 30s test @ http://localhost:3000/user/123
|
|
154
|
+
8 threads and 100 connections
|
|
155
|
+
Thread Stats Avg Stdev Max +/- Stdev
|
|
156
|
+
Latency 1.33ms 690.36us 30.28ms 97.16%
|
|
157
|
+
Req/Sec 9.27k 1.09k 11.76k 89.58%
|
|
158
|
+
2214097 requests in 30.02s, 276.61MB read
|
|
159
|
+
Requests/sec: 73754.53
|
|
160
|
+
Transfer/sec: 9.21MB
|
|
161
|
+
```
|
|
127
162
|
|
|
128
|
-
See [benchmark](/benchmark)
|
|
163
|
+
See more details, check [benchmark](/benchmark) section.
|
|
129
164
|
|
|
130
165
|
## Related
|
|
131
166
|
|
|
132
167
|
- [send-http](https://github.com/Kikobeats/send-http) – A `res.end` with data type detection.
|
|
133
168
|
- [http-body](https://github.com/Kikobeats/http-body) – Parse the http.IncomingMessage body into text/json/buffer.
|
|
134
|
-
- [http-compression](https://github.com/Kikobeats/http-compression) – Adding compression (gzip/brotli) for your HTTP server in Node.js
|
|
135
|
-
- [to-query](https://github.com/Kikobeats/to-query) Get query object from a request url.
|
|
169
|
+
- [http-compression](https://github.com/Kikobeats/http-compression) – Adding compression (gzip/brotli) for your HTTP server in Node.js.
|
|
136
170
|
|
|
137
171
|
## License
|
|
138
172
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "router-http",
|
|
3
3
|
"description": "Simple HTTP router compatible with Express",
|
|
4
4
|
"homepage": "https://nicedoc.io/Kikobeats/router-http",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.2.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"routes"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"trouter": "~3.2.
|
|
31
|
+
"trouter": "~3.2.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@commitlint/cli": "latest",
|
package/src/index.js
CHANGED
|
@@ -56,13 +56,13 @@ class Router extends Trouter {
|
|
|
56
56
|
* .use('/v2', two)
|
|
57
57
|
*/
|
|
58
58
|
use = (base = '/', ...fns) => {
|
|
59
|
-
if (typeof base === 'function') {
|
|
60
|
-
this.#middlewares = this.#middlewares.concat(base, fns)
|
|
59
|
+
if (typeof base === 'function' || typeof base === 'boolean') {
|
|
60
|
+
this.#middlewares = this.#middlewares.concat(base, fns).filter(Boolean)
|
|
61
61
|
} else if (base === '/') {
|
|
62
|
-
this.#middlewares = this.#middlewares.concat(fns)
|
|
62
|
+
this.#middlewares = this.#middlewares.concat(fns).filter(Boolean)
|
|
63
63
|
} else {
|
|
64
64
|
base = lead(base)
|
|
65
|
-
fns.forEach(fn => {
|
|
65
|
+
fns.filter(Boolean).forEach(fn => {
|
|
66
66
|
const array = this.#middlewaresBy[base] ?? []
|
|
67
67
|
// eslint-disable-next-line no-sequences
|
|
68
68
|
array.length > 0 || array.push((r, _, nxt) => (mutate(base, r), nxt()))
|