router-http 0.0.0 → 0.1.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 +10 -13
- package/package.json +4 -4
- package/src/index.js +18 -4
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
# http
|
|
1
|
+
# router-http
|
|
2
2
|
|
|
3
|
-

|
|
4
|
+
[](https://coveralls.io/github/Kikobeats/router-http)
|
|
5
|
+
[](https://www.npmjs.org/package/router-http)
|
|
6
6
|
|
|
7
7
|
An HTTP router focused in only that, similar to [express@router](https://github.com/pillarjs/router), but:
|
|
8
8
|
|
|
9
9
|
- Focused in just one thing.
|
|
10
10
|
- Maintained and well tested.
|
|
11
|
-
-
|
|
12
|
-
-
|
|
11
|
+
- Smaller and portable (1.4 kB).
|
|
12
|
+
- Most of router API is supported.
|
|
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
|
|
|
@@ -17,14 +17,11 @@ While I was evaluating the market for finding an alternative I found [polka](htt
|
|
|
17
17
|
|
|
18
18
|
- This module doesn't take care about the http.Server.
|
|
19
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.
|
|
20
|
-
- This module doesn't adds `req.query` nor `req.search` (check [to-query](https://github.com/Kikobeats/to-query) for that).
|
|
21
|
-
|
|
22
|
-
In resume: this module does nothing beyond finding the correct path and matching the associated code.
|
|
23
20
|
|
|
24
21
|
## Install
|
|
25
22
|
|
|
26
23
|
```bash
|
|
27
|
-
$ npm install http
|
|
24
|
+
$ npm install router-http --save
|
|
28
25
|
```
|
|
29
26
|
|
|
30
27
|
## Usage
|
|
@@ -32,7 +29,7 @@ $ npm install http-router --save
|
|
|
32
29
|
First, you should to create a router:
|
|
33
30
|
|
|
34
31
|
```js
|
|
35
|
-
const createRouter = require('http
|
|
32
|
+
const createRouter = require('router-http')
|
|
36
33
|
|
|
37
34
|
const router = createRouter((err, req, res) => {
|
|
38
35
|
const hasError = err !== undefined
|
|
@@ -142,7 +139,7 @@ See [benchmark](/benchmark) sections.
|
|
|
142
139
|
|
|
143
140
|
## License
|
|
144
141
|
|
|
145
|
-
**http
|
|
146
|
-
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/http
|
|
142
|
+
**router-http** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/router-http/blob/master/LICENSE.md) License.<br>
|
|
143
|
+
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/router-http/contributors).
|
|
147
144
|
|
|
148
145
|
> [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "router-http",
|
|
3
3
|
"description": "Simple HTTP router compatible with Express",
|
|
4
|
-
"homepage": "https://nicedoc.io/Kikobeats/http
|
|
5
|
-
"version": "0.
|
|
4
|
+
"homepage": "https://nicedoc.io/Kikobeats/router-http",
|
|
5
|
+
"version": "0.1.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/Kikobeats/http
|
|
14
|
+
"url": "git+https://github.com/Kikobeats/router-http.git"
|
|
15
15
|
},
|
|
16
16
|
"bugs": {
|
|
17
|
-
"url": "https://github.com/Kikobeats/http
|
|
17
|
+
"url": "https://github.com/Kikobeats/router-http/issues"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
20
20
|
"app",
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,17 @@ const value = x => {
|
|
|
16
16
|
return y > 1 ? x.substring(0, y) : x
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
const parse = ({ url }) => {
|
|
20
|
+
const index = url.indexOf('?', 1)
|
|
21
|
+
const obj = { pathname: url, query: null, search: null }
|
|
22
|
+
if (index !== -1) {
|
|
23
|
+
obj.search = url.substring(index)
|
|
24
|
+
obj.query = obj.search.substring(1)
|
|
25
|
+
obj.pathname = url.substring(0, index)
|
|
26
|
+
}
|
|
27
|
+
return obj
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
const mutate = (str, req) => {
|
|
20
31
|
req.url = req.url.substring(str.length) || '/'
|
|
21
32
|
req.path = req.path.substring(str.length) || '/'
|
|
@@ -53,6 +64,7 @@ class Router extends Trouter {
|
|
|
53
64
|
base = lead(base)
|
|
54
65
|
fns.forEach(fn => {
|
|
55
66
|
const array = this.#middlewaresBy[base] || []
|
|
67
|
+
// eslint-disable-next-line no-sequences
|
|
56
68
|
array.length > 0 || array.push((r, _, nxt) => (mutate(base, r), nxt()))
|
|
57
69
|
this.#middlewaresBy[base] = array.concat(fn)
|
|
58
70
|
})
|
|
@@ -60,12 +72,12 @@ class Router extends Trouter {
|
|
|
60
72
|
return this
|
|
61
73
|
}
|
|
62
74
|
|
|
63
|
-
handler = (req, res,
|
|
64
|
-
|
|
75
|
+
handler = (req, res, info) => {
|
|
76
|
+
info = info || parse(req)
|
|
65
77
|
let fns = []
|
|
66
78
|
let middlewares = this.#middlewares
|
|
67
|
-
const route = this.find(req.method, pathname)
|
|
68
|
-
const base = value((req.path = pathname))
|
|
79
|
+
const route = this.find(req.method, info.pathname)
|
|
80
|
+
const base = value((req.path = info.pathname))
|
|
69
81
|
if (this.#middlewaresBy[base] !== undefined) {
|
|
70
82
|
middlewares = middlewares.concat(this.#middlewaresBy[base])
|
|
71
83
|
}
|
|
@@ -74,6 +86,8 @@ class Router extends Trouter {
|
|
|
74
86
|
req.params = { ...req.params, ...route.params }
|
|
75
87
|
}
|
|
76
88
|
fns.push(this.unhandler)
|
|
89
|
+
req.search = info.search
|
|
90
|
+
req.query = info.query
|
|
77
91
|
// Exit if only a single function
|
|
78
92
|
let i = 0
|
|
79
93
|
let len = middlewares.length
|