router-http 0.1.0 → 0.1.2
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 +3 -6
- package/package.json +1 -1
- package/src/index.js +23 -19
package/README.md
CHANGED
|
@@ -13,10 +13,7 @@ An HTTP router focused in only that, similar to [express@router](https://github.
|
|
|
13
13
|
|
|
14
14
|
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
15
|
|
|
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 module is different
|
|
17
|
-
|
|
18
|
-
- This module doesn't take care about the http.Server.
|
|
19
|
-
- This module doesn't use any of the Node.js built-in module, so it can be used in Vercel Edge Functions, Deno or CF Workers.
|
|
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 module is different from polka in it isn't taking care about http.Server, it just acts as an isolated module.
|
|
20
17
|
|
|
21
18
|
## Install
|
|
22
19
|
|
|
@@ -31,8 +28,8 @@ First, you should to create a router:
|
|
|
31
28
|
```js
|
|
32
29
|
const createRouter = require('router-http')
|
|
33
30
|
|
|
34
|
-
const router = createRouter((
|
|
35
|
-
const hasError =
|
|
31
|
+
const router = createRouter((error, req, res) => {
|
|
32
|
+
const hasError = error !== undefined
|
|
36
33
|
res.statusCode = hasError ? 500 : 404
|
|
37
34
|
res.end(hasError ? err.message : 'Not Found')
|
|
38
35
|
})
|
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.1.
|
|
5
|
+
"version": "0.1.2",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
package/src/index.js
CHANGED
|
@@ -28,8 +28,8 @@ const parse = ({ url }) => {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const mutate = (str, req) => {
|
|
31
|
-
req.url = req.url.substring(str.length)
|
|
32
|
-
req.path = req.path.substring(str.length)
|
|
31
|
+
req.url = req.url.substring(str.length) ?? '/'
|
|
32
|
+
req.path = req.path.substring(str.length) ?? '/'
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
class Router extends Trouter {
|
|
@@ -63,7 +63,7 @@ class Router extends Trouter {
|
|
|
63
63
|
} else {
|
|
64
64
|
base = lead(base)
|
|
65
65
|
fns.forEach(fn => {
|
|
66
|
-
const array = this.#middlewaresBy[base]
|
|
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()))
|
|
69
69
|
this.#middlewaresBy[base] = array.concat(fn)
|
|
@@ -73,7 +73,7 @@ class Router extends Trouter {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
handler = (req, res, info) => {
|
|
76
|
-
info = info
|
|
76
|
+
info = info ?? parse(req)
|
|
77
77
|
let fns = []
|
|
78
78
|
let middlewares = this.#middlewares
|
|
79
79
|
const route = this.find(req.method, info.pathname)
|
|
@@ -86,29 +86,33 @@ class Router extends Trouter {
|
|
|
86
86
|
req.params = { ...req.params, ...route.params }
|
|
87
87
|
}
|
|
88
88
|
fns.push(this.unhandler)
|
|
89
|
-
req.search = info.search
|
|
90
|
-
req.query = info.query
|
|
89
|
+
req.search = req.query ?? info.search
|
|
90
|
+
req.query = req.query ?? info.query
|
|
91
91
|
// Exit if only a single function
|
|
92
|
-
let
|
|
93
|
-
let
|
|
92
|
+
let index = 0
|
|
93
|
+
let size = middlewares.length
|
|
94
94
|
const num = fns.length
|
|
95
|
-
if (
|
|
95
|
+
if (size === index && num === 1) return fns[0](undefined, req, res)
|
|
96
96
|
|
|
97
97
|
// Otherwise loop thru all middlware
|
|
98
98
|
const next = err => (err ? this.unhandler(err, req, res, next) : loop())
|
|
99
99
|
|
|
100
|
-
const loop = () =>
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
const loop = () =>
|
|
101
|
+
res.writableEnded ||
|
|
102
|
+
(index < size &&
|
|
103
|
+
(() => {
|
|
104
|
+
try {
|
|
105
|
+
const mware = middlewares[index++]
|
|
106
|
+
return index === size
|
|
107
|
+
? mware(undefined, req, res, next)
|
|
108
|
+
: mware(req, res, next)
|
|
109
|
+
} catch (err) {
|
|
110
|
+
return this.unhandler(err, req, res, next)
|
|
111
|
+
}
|
|
112
|
+
})())
|
|
109
113
|
|
|
110
114
|
middlewares = middlewares.concat(fns)
|
|
111
|
-
|
|
115
|
+
size += num
|
|
112
116
|
loop() // init
|
|
113
117
|
}
|
|
114
118
|
}
|