router-http 0.1.0 → 0.1.1
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 +2 -2
- package/package.json +1 -1
- package/src/index.js +6 -6
package/README.md
CHANGED
|
@@ -31,8 +31,8 @@ First, you should to create a router:
|
|
|
31
31
|
```js
|
|
32
32
|
const createRouter = require('router-http')
|
|
33
33
|
|
|
34
|
-
const router = createRouter((
|
|
35
|
-
const hasError =
|
|
34
|
+
const router = createRouter((error, req, res) => {
|
|
35
|
+
const hasError = error !== undefined
|
|
36
36
|
res.statusCode = hasError ? 500 : 404
|
|
37
37
|
res.end(hasError ? err.message : 'Not Found')
|
|
38
38
|
})
|
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.1",
|
|
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,8 +86,8 @@ 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
92
|
let i = 0
|
|
93
93
|
let len = middlewares.length
|