Restify Router
This module allows you to define your routes using a Router interface that is identical to how routes are registered on a restify server. You can then apply the routes to a server instance.
Borrowing from the idea of Express router where you can organize routes by creating multiple routers and applying them to an express server, this component allows you to achieve a similar separation/grouping of route definitions.
Summary
Installation
$ npm install --save restify-router
Creating a router
A router object is an isolated instance of routes. The router interface matches the interface for adding routes to a restify server:
var Router = Router;var routerInstance = ;var restify = ; { res; ;} // add a route like you would on a restify server instancerouterInstance; var server = restify;// add all routes registered in the router to this server instancerouterInstance; server;
Why use it?
When your application starts to contain a lot of routes, you may want to group the definition of routes in separate files rather than registering every route in a single server bootstrap/creation file.
For example, if we have two sets of routes in our application:
Users:
- GET
/users
- GET
/users/:id
Posts:
- GET
/posts
- GET
/posts/:id
var userRouter = ; // return a Router with only user route definitionsvar postsRouter = ; // return a Router with only posts route definitions var restify = ;var server = restify; // add user routesuserRouter; // add posts routespostsRouter; server;
Prefixing Routes
To prefix all routes, specify the prefix as the second argument to router.applyRoutes(server, prefix)
prefix
must be a string or a regex
Example:
Routes:
- GET
/admin/settings
- GET
/admin/controls
var Router = Router;var restify = ; { res; ;} { res; ;} var routerInstance = ; // add a route like you would on a restify server instancerouterInstance;routerInstance; var server = restify;// add all routes registered in the router to this server instance with uri prefix 'admin'routerInstance; server;
Nesting Routers
If you are familiar with Express style routers, you have the ability to nest routers under other routers to create a hierarchy of route definitions.
To nest routers use the .add
method on a Router:
router;
- path - a string or regexp path beginning with a forward slash (/)
- All routes defined in the provided router will be prefixed with this path during registration
- router - the router instance to nest
Example Usage
// routes/v1/auth.js const router = ;router; moduleexports = router;
// routes/v1/routes.js const router = ;router; moduleexports = router;
// routes/routes.js const router = ;router; moduleexports = router;
With the above router definition from routes/routes.js
we can do the following call:
POST /v1/auth/register
This call is possible because we have nested routers two levels deep from the /v1
path.
Grouping Routers
As an alternative to Nesting Routers, you can use the group to clarify the middlewares manipulation and the routes / files organization. Works in a way that does not need to create multiple instances of the Router like Nesting.
To group routers use the .group
method on a Router:
router;
Example Usage
Basic Usage
var Router = Router;var restify = ; var routerInstance = ;var server = restify; routerInstance; routerInstance; routerInstance; // add all routes registered in the router to this server instancerouterInstance; server;
With the above code definition we can do the following calls:
- GET
/
- GET
/v1
- POST
/v1/auth/register
- GET
/v2
Basic Usage with nesting Middlewares
var Router = Router;var restify = ; var routerInstance = ;var server = restify; { /**/ } { /**/ } { /**/ } routerInstance; // add all routes registered in the router to this server instancerouterInstance; server;
With the above code definition we can do the following calls:
- GET
/v1 [midFirst]
- POST
/v1/auth/register [midFirst, midSecond, midThird]
Common Middleware
There may be times when you want to apply some common middleware to all routes registered with a router. For example, you may want some common authorization middleware for all routes under a specific router.
All middleware registered via .use
will be applied before route level middleware.
To stay consistent with the restify
server interface, the method on the Router is:
.use(middlewareFn, middlewareFn2, ...)
.use([middlewareFn, middlewareFn2, ...])
Note: Multiple calls to .use
will result in aggregation of middleware, each successive call will append to the list of common middleware
Example Usage
var router = ; // this will run before every route on this routerrouter; router; router; router; // calling GET /hello runs use middle ware first and then the routes middleware
Links
For more information about Restify Router see Organizing Restify Routes with Restify Router