restify-router-config
The laziest way to route your restify app.
This is an effort to maximize laziness in routing restify app. This tool includes grouping of routes, sorting of routes, and applying multiple middleware in most convenient way, of course the structure is inspired by react-router-config.
NOTE: This module is also compatible with express but usage may vary.
Features:
- Sorting - will sort your routes, wildcards has been taken into account too.
- Grouping - will add a prefix to your routes depending on the group.
- Multiple Middleware - will let you add one or more middleware in most convenient way.
- Pre/Post Middleware - let's you define whether the middleware should execute before or after executing your function
- Nesting groups - allows you to nest your groups.
Installation
npm install -S restify-router-config
Usage
/** create restify server */const server = restify/** configure your routes */// NOTE: you may or may not use group, but for the sake of grouping up// your endpoints it's best to use it. group: 'users' routes: match: '/:id' // would generate /users/:id route middleware: restrictedRoute method: 'get' action: getUserById match: '/' // would generate /users method: 'get' action: getUser match: '/login' // would generate login method: 'post' action: login server
Nesting groups
const router = const restify = const apiAuth = { console; } const loggingMW = { console } const logDone = { console } group: 'api/v1' middleware: apiAuth routes: match: '/hello' method: 'get' res group: 'users' middleware: 'before' loggingMW 'after' logDone routes: match: '/:id' method: 'get' { res } match: '/:id/friends' method: 'get' { res } match: '/' method: 'get' { res } server
The example above would generate the following routes:
[get] - /api/v1/users/:id/friends
[get] - /api/v1/users/:id
[get] - /api/v1/hello
[get] - /api/v1/users
Setting middleware's execution
You may choose to add middleware after the function's execution, in order to do that you must use the following syntax:
/** create restify server */const server = restify/** configure your routes */// NOTE: you may or may not use group, but for the sake of grouping up// your endpoints it's best to use it. match: '/users/:id' method: 'get' middleware: 'before' authenticate 'after' logger action: getUserById server
in the example above, the /users/:id
route is authenticated first, then executes getUserById
, after the execution of the getUserById
the logger
is executed right away.
Why use restify-router-config?
You don't have to, but if you're used to react-router-config
, then this would make it easier for you to configure your routes as
it is almost similar structure to react-router-config
, another advantage of using this route tool is you can easily chain your middleware. For instance if you want to protect all of your routes, and at same time a single route would require additional middleware, you'd do it like this:
// assuming that you want to protect all routes under /users group: 'users' middleware: restrictedRoute routes: match: '/:id' middleware: anotherMiddleware method: 'get' action: getUserById match: '/' method: 'get' action: getUser
the code from above would use restrictedRoute
middleware first, and if you're going to access /users/:id,
it would also use anotherMiddleware
middleware.