express-route-dispatcher
Micro routing framework on top of the Express framework.
[2014-01-28] Now with generators/yield support
What does it do?
ERD provides a way of decoupling your route definition and the actual implementation by following a convention based approach. Furthermore it allows you to add "before" hooks to your route processing.
The routes are mapped to controllers/actions (files/methods).
See the examples for live action.
Install
$ npm install express express-route-dispatcher
Examples 1: basic routing
ERD assumes some defaults when not explicitely configured. For example it expects the controllers to be in a directory called "./controllers".
Example directory layout ./server.js ./controllers/basic.js ./controllers/auth.js
./server.js
var express = ; var dispatcher = ; var app = ; // create the routes dispatcher; // apply the routing configuration dispatcher;
./controllers/basic.js
moduleexports = { if return ; res; } { res; } { res; } { try var data = ; var version = ; res; catcherr res; }
Example 2: with options
ERD supports two options
- path (where to find the controllers)
- parseControllerName (allows you to adjust the lookup algorithm)
Example directory layout ./server.js ./api/basicController.js
./server.js
// ... dispatcher; dispatcher; dispatcher;
Example 3: nested routing
ERD supports nested routing which might be useful for authentication checks and alike. All handlers inside the new map() will run through the before check first.
./server.js
// ... dispatcher;