Skip to content

Commit cf0dd9d

Browse files
authored
Router query string support (#8)
* v2.1.0 * ignore query strings during requests matching * allowing prefix to be optional in global middlewares * improving test cases
1 parent e4eb781 commit cf0dd9d

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

lib/router/sequential.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,27 @@ module.exports = (config = {}) => {
1515
const router = new Trouter()
1616
router.id = config.id
1717

18-
router.lookup = (req, res, step) => {
19-
if (req.originalUrl === undefined) {
20-
req.originalUrl = req.originalUrl || req.url
18+
router._use = router.use
19+
20+
router.use = (prefix, ...middlewares) => {
21+
if (typeof prefix === 'function') {
22+
middlewares = prefix
23+
prefix = '/'
2124
}
25+
router._use(prefix, middlewares)
26+
27+
return this
28+
}
29+
30+
router.lookup = (req, res, step) => {
31+
req.url = req.url || '/'
32+
req.originalUrl = req.originalUrl || req.url
33+
req.path = req.url.split('?')[0]
2234

23-
const reqCacheKey = `${req.method + req.url}`
35+
const reqCacheKey = `${req.method + req.path}`
2436
let match = cache.get(reqCacheKey)
2537
if (!match) {
26-
match = router.find(req.method, req.url)
38+
match = router.find(req.method, req.path)
2739
cache.set(reqCacheKey, match)
2840
}
2941

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "0http",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Cero friction HTTP request router. The need for speed!",
55
"main": "index.js",
66
"scripts": {
@@ -18,6 +18,9 @@
1818
"router",
1919
"rest"
2020
],
21+
"engines": {
22+
"node": ">=10.x"
23+
},
2124
"author": "Rolando Santamaria Maso <[email protected]>",
2225
"license": "MIT",
2326
"bugs": {

tests/nested-routers.test.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('0http Web Framework - Nested Routers', () => {
66
const baseUrl = 'http://localhost:' + process.env.PORT
77

88
const { router, server } = require('../index')({
9-
server: require('../lib/server/low')(),
109
router: require('../lib/router/sequential')()
1110
})
1211

@@ -31,10 +30,8 @@ describe('0http Web Framework - Nested Routers', () => {
3130
next()
3231
})
3332

34-
server.start(~~process.env.PORT, serverSocket => {
35-
if (serverSocket) {
36-
done()
37-
}
33+
server.listen(~~process.env.PORT, err => {
34+
if (!err) done()
3835
})
3936
})
4037

@@ -56,10 +53,10 @@ describe('0http Web Framework - Nested Routers', () => {
5653
})
5754

5855
await request(baseUrl)
59-
.get('/r2/url')
56+
.get('/r2/url?var=value')
6057
.expect(200)
6158
.then((response) => {
62-
expect(response.text).to.equal('/r2/url:/url')
59+
expect(response.text).to.equal('/r2/url?var=value:/url?var=value')
6360
})
6461
})
6562

tests/smoke.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('0http Web Framework - Smoke', () => {
1111
})
1212

1313
it('should successfully register service routes', (done) => {
14+
router.use((req, res, next) => next())
15+
router.use('/', (req, res, next) => next())
16+
1417
router.get('/pets/:id', function (req, res) {
1518
res.setHeader('content-type', 'application/json')
1619
res.end(JSON.stringify({
@@ -66,7 +69,7 @@ describe('0http Web Framework - Smoke', () => {
6669

6770
it('should GET JSON response /pets/:id', async () => {
6871
await request(baseUrl)
69-
.get('/pets/0')
72+
.get('/pets/0?var=value')
7073
.expect(200)
7174
.then((response) => {
7275
expect(response.body.name).to.equal('Happy Cat')

0 commit comments

Comments
 (0)