dynamic-routes
A dynamic router / loader for http://expressjs.com/
Install
npm install dynamic-routes --save
Usage
DynamicRoutes(app, path_to_routes, [options])
Examples :
Express.JS Server :
var express = http = path = DynamicRoutes = app = ; app;app;app;app;app;app;app;app;app; if 'development' == app app; ;// or /*DynamicRoutes(app, { src: __dirname + '/routes/', ignore: [], pattern: /\.js$/});*/ http;
Creating compatible routes :
Using an object :
//routes/index.jsmoduleexports = priority: 1000 //this is the `/` handler, therefore it should be the last route. path: '/' //this function gets passed the express object one time for any extra setup { } //a middleware that is called from any HTTP methods { res; ; } //HTTP method specific routes { res; } { res; res; } //routes supports GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE, PATCH method(all uppercase);
Routing multiple middlewares by assigning an array of middlewares :
moduleexports = priority: 1000 path: '/' { } ALL: { res; ; } { res; ; } GET: { res; ; } { res; } POST: { res; res; } /* an array with single route, that is equivalent to POST: function(req, res){ res.json(req.body); res.end('end of POST'); } */
Using a function :
var user = module { "use strict"; //other setup app;}; userpriority = 1;userpath = '/user/:id?';
This is equivalent to :
moduleexports = path: '/user/:id?' priority: 1 { //setup db } { res; }
Notes
- As of Express 4.x, it's recommended to use
require('body-parser')()
instead ofexpress.bodyParser()
, checkthis
.