Introduction
This is a module for restify.js to build nested routers. Nested routers can help us to separate a big routing map into well structured pieces according to there coresponding modules.
Quick Start
var restify = server = restify serverserver var RestifyRouter = var rootRouter = rootRouter // Sub Routersvar subRouter = subRouter // Build subRouter under sub-path '/user'// this will add restify native route map '/user/:username'rootRouter // From version 0.3.3, endpoints will not get applied until explicitly call **applyRoutes**// and CAN NOT be modified anymore.rootRouter server
You can also try the climbPathTree
method to define and generate the whole routing map in one pass,
which is illustrated in version 0.2.*
, also can be find in the file tests/index.js
This module is used in Goyoo OEM
project right now, please feel free to post issues and merge requests.
Enjoy.
Middleware
// For things need to be done before end points, typially middlewares// From version 0.3.*, chained handlers is supported// This will insert two middlewares in sequence before every end points subRouterall'/user/*' { // something to do ... ; // remember to invoke next(), or send a response to break the callback chain} { // some other interesting things to do ... ;};
What's new
version 0.3.*
- Use
.all(path, callback, ... )
to set middlewares as Express style, all middleware will be invoked in sequence before the end points, just like the.all
syntax in Express - Actions can be chained like:
router.all('/user', middleware1, middleware2, middleware3).get('/user', cb1, cb2).post('/service', cb) ....
- Support chained handlers like this:
router.get('/path', cb1, cb2, cb3)
- Support
*
syntax when setting middleware:router.all('/user/*', cb1, cb2)
version 0.2.*
- Test is ready
.climbPathTree()
function is added, to parse a routing path tree, which defined as the example shown below:
let pathTree = subPaths: user: subPaths: ':username': allowedMethods: get : fakeController // the value is a function to handle restify routing requests put : fakeController del : fakeController patch : fakeController post : fakeController service: subPaths: orderSeat: allowedMethods: put : fakeController all : preCheckForAllMethods subPaths: ':orderId' : allowedMethods: get: fakeController delete: fakeController patch: fakeController orderMeal: allowedMethods: put: fakeController subPaths: ':orderId' : allowedMethods: get: fakeController delete: fakeController patch: fakeController login: allowedMethods: get: fakeController post: fakeController register: allowedMethods: get: fakeController post: fakeController allowedMethods: get: fakeController { reqcount = 1; return } { reqcount = reqcount?reqcount+1:1; res}
version 0.1.*
Nested routing use syntax .use()
like Express