Trie-based URL and Method Routing for Node.js
Dispatchington is a trie-based URL and method router for Node.js based on routington. It can almost be used as a drop-in replacement. For information on its motivations, read about The Imperfections of Express.
When should you use this?
405 Method Not Allowedautomatically.
501 Not Implementedautomatically, assuming you use the middleware.
/benchmark for the benchmark test. The take away of this benchmark isn't that Dispatchington is faster at routing, but that its performance is more precise. In other words, it "scales" with the number and complexity of routes.
Note that these benchmarks are not comprehensive and that the differences in speeds are due to more just the route being matched.
||10804 req/s||10703 req/s|
||8862 req/s||10329 req/s|
||9294 req/s||10493 req/s|
var dispatchington = require'dispatchington'var router = dispatchington
To mount the middleware:
var app = expressappuserouterimplementedMethods// static stuff// cookie stuff// session stuffappuserouterdispatcher// 404 handler// error handler
If you're not using Express or Connect, it will still work:
var dispatcher = routerdispatchervar methods = routermethods// ...// Define routes// ...httpcreateServeron'request'if !methodsreqmethodtoLowerCaseresstatusCode = 501resend'Not Implemented'returndispatcherif errresstatusCode = errstatus || 500resenderrmessage || 'Internal Server Error'return// If this point is reached,// no route has been matchedresstatusCode = 404resend'Not Found'listenprocessenvPORT || 80
You will define middleware stacks which will then be referrable by name. For example:
routerget'/' 'compress' 'session'
is equivalent to all of the following:
routerget'/' connectcompress connectcookieParser connectcookieSessionrouterget'/' connectcompressconnectcookieParserconnectcookieSessionrouterget'/'connectcompressconnectcookieParserconnectcookieSession
The idea is that you turn your middleware stacks into legos. You can define them early in your app, then re-use them throughout your routes without
require()ing each of them from an external "common" module. It will also make your routes much more readable as, ideally, it could look something like this:
routerget'/''compress''cache if visitor''accept html only''retrieve session''retrieve user''check if allowed''parse body'handlerresrender'homepage'
For information on how the routing strings actually work, view routington's documentation.
There are two ways to create a route. The first is Express' method-first way:
routerget'/things' 'compress' 'session' handlerrouterpost'/things' 'compress' 'session' handlerrouterput'/things' 'compress' 'session' handler
The other is by defining a route, then adding stacks based on the method:
routerroute'/things'get'compress' 'session' handlerpost'compress' 'session' handlerput'compress' 'session' handler
In both versions, you can define multiple routes at the same time using an array:
routerroute'/things''/thing/:id'get'compress' 'session' handlerpost'compress' 'session' handlerput'compress' 'session' handler
If you define a route like so:
You can retrieve the value of
id just like in Express:
reqid = reqparamsidnext
The trie, specifically the routington instance, is accessible at
router.trie. You can manipulate it however you'd like. View its documentation for more information.
app.allis not supported. Simply define a stack and use it in every method.
npm install methodsfor a list of all possible methods.
© Jonathan Ong 2013