Hi, I've worked on restify and express few months ago, and I find it hassle to route my app in such way that I have to order them manual. So, I started working on a small tool that would sort routes for me and manage all of my middlewares.
Behold, restify-router-config.
It lets you handle various routing task in more efficient way e.g. (nested groups, middleware management, sorting routes based on wildcards)
Here's a simple usage example:
/**
* Note that the goal of this snippet is to showcase the usage of the
* tool, code provided is not from actual project.
*/
const router = require('restify-router-config')
const restify = require('restify')
const server = restify.createServer()
const apiAuth = (req, res, next) => {
console.log('authed!');
next()
}
const loggingMW = (req, res, next) => {
console.log(req._timeStart)
next()
}
const logDone = (req, res, next) => {
console.log('done!')
next()
}
router(server, true) ([
{
group: 'api/v1',
middleware: apiAuth,
routes: [
{
match: '/hello',
method: 'get',
action: (req, res, next) => res.send('hello')
},
{
group: 'users',
middleware: [
['before', loggingMW],
['after', logDone]
],
routes: [
{
match: '/:id',
method: 'get',
action: (req, res, next) => {
res.send('hello')
next()
}
},
{
match: '/:id/friends',
method: 'get',
action: (req, res, next) => {
res.send('hello')
next()
}
},
{
match: '/',
method: 'get',
action: (req, res, next) => {
res.send('hello')
next()
}
}
]
}
]
}
])
server.listen(4000)
If you think this tool is useful or not, please let me know, I really think it's convenient but what do you guys think? BTW The usage shown from above is using restify, this tool is also compatible with express but some usage may vary.
Top comments (0)