Skip to content

Commit

Permalink
Router query string support (#8)
Browse files Browse the repository at this point in the history
* v2.1.0
* ignore query strings during requests matching
* allowing prefix to be optional in global middlewares
* improving test cases
  • Loading branch information
jkyberneees authored Dec 30, 2019
1 parent e4eb781 commit cf0dd9d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
22 changes: 17 additions & 5 deletions lib/router/sequential.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ module.exports = (config = {}) => {
const router = new Trouter()
router.id = config.id

router.lookup = (req, res, step) => {
if (req.originalUrl === undefined) {
req.originalUrl = req.originalUrl || req.url
router._use = router.use

router.use = (prefix, ...middlewares) => {
if (typeof prefix === 'function') {
middlewares = prefix
prefix = '/'
}
router._use(prefix, middlewares)

return this
}

router.lookup = (req, res, step) => {
req.url = req.url || '/'
req.originalUrl = req.originalUrl || req.url
req.path = req.url.split('?')[0]

const reqCacheKey = `${req.method + req.url}`
const reqCacheKey = `${req.method + req.path}`
let match = cache.get(reqCacheKey)
if (!match) {
match = router.find(req.method, req.url)
match = router.find(req.method, req.path)
cache.set(reqCacheKey, match)
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "0http",
"version": "2.0.0",
"version": "2.1.0",
"description": "Cero friction HTTP request router. The need for speed!",
"main": "index.js",
"scripts": {
Expand All @@ -18,6 +18,9 @@
"router",
"rest"
],
"engines": {
"node": ">=10.x"
},
"author": "Rolando Santamaria Maso <kyberneees@gmail.com>",
"license": "MIT",
"bugs": {
Expand Down
11 changes: 4 additions & 7 deletions tests/nested-routers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('0http Web Framework - Nested Routers', () => {
const baseUrl = 'http://localhost:' + process.env.PORT

const { router, server } = require('../index')({
server: require('../lib/server/low')(),
router: require('../lib/router/sequential')()
})

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

server.start(~~process.env.PORT, serverSocket => {
if (serverSocket) {
done()
}
server.listen(~~process.env.PORT, err => {
if (!err) done()
})
})

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

await request(baseUrl)
.get('/r2/url')
.get('/r2/url?var=value')
.expect(200)
.then((response) => {
expect(response.text).to.equal('/r2/url:/url')
expect(response.text).to.equal('/r2/url?var=value:/url?var=value')
})
})

Expand Down
5 changes: 4 additions & 1 deletion tests/smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('0http Web Framework - Smoke', () => {
})

it('should successfully register service routes', (done) => {
router.use((req, res, next) => next())
router.use('/', (req, res, next) => next())

router.get('/pets/:id', function (req, res) {
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({
Expand Down Expand Up @@ -66,7 +69,7 @@ describe('0http Web Framework - Smoke', () => {

it('should GET JSON response /pets/:id', async () => {
await request(baseUrl)
.get('/pets/0')
.get('/pets/0?var=value')
.expect(200)
.then((response) => {
expect(response.body.name).to.equal('Happy Cat')
Expand Down

0 comments on commit cf0dd9d

Please sign in to comment.