Skip to content

Commit

Permalink
Merge pull request #20 from jkyberneees/express-like-route-middlewares
Browse files Browse the repository at this point in the history
Express like route middlewares
  • Loading branch information
jkyberneees authored Feb 3, 2019
2 parents 62fece2 + e52c197 commit aa7aec6
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 93 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ service.get('/hi/:name', async (req, res) => {
next()
}]) // route middlewares can be passed in an Array after the handler context param
```
Express.js like signature also supported:
```js
service.get('/hi/:name', m1, m2, handler [, ctx])
```

### Sending custom headers:
```js
Expand Down
17 changes: 17 additions & 0 deletions demos/route-middleware-express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const service = require('../index')({})

// route without middlewares
service.get('/hi/:name', async (req, res) => {
return 'Hello ' + req.params.name
})

// route with middlewares using express like signature
service.get('/hi-m/:name', (req, res, next) => {
req.params.name = req.params.name.toUpperCase()
next()
}, async (req, res) => {
return 'Hello ' + req.params.name
})

// start the server
service.start()
10 changes: 0 additions & 10 deletions demos/route-middlewares.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
const service = require('../index')({})

service.use((req, res, next) => {
const now = new Date().getTime()

res.on('response', (e) => {
e.res.setHeader('x-response-time', new Date().getTime() - now)
})

return next()
})

// route without middlewares
service.get('/hi/:name', async (req, res) => {
return 'Hello ' + req.params.name
Expand Down
31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,37 @@ module.exports = (options = {}) => {
}

// exposing HTTP verbs as request routing methods
// express.js like routes middlewares signature is supported: app.get('/', m1, m2, handler)
methods.forEach((method) => {
app[method] = (path, handler, ctx, middlewares = []) => app.route(method.toUpperCase(), path, handler, ctx, middlewares)
app[method] = (path, ...handlers) => {
let ctx = {}
let middlewares = []

// try handler as last element of the array
let handler = handlers.pop()

if (Array.isArray(handler) && handler.length && typeof handler[0] === 'function') {
// route middlewares are remaining elements
middlewares.push(...handler)
// handler is fist element
handler = handlers.shift()
// ctx is second element
ctx = handlers.shift()
} else if (typeof handler !== 'function') {
// last element is not a function, should be handler ctx
ctx = handler
// route handler is remaining element
handler = handlers.pop()
}

if (!middlewares.length) {
// route middlewares are remaining elements
middlewares.push(...handlers)
}

// register route
app.route(method.toUpperCase(), path, handler, ctx, middlewares)
}
})

// integrator callback
Expand Down
Loading

0 comments on commit aa7aec6

Please sign in to comment.