express-dynamic-router
Dynamic routing system for express.
author : Yuki Takei(MikeTOKYO)
email : yuki@miketokyo.com
Overseas
express-dynamic-router provide you the auto routing system for express.
You don't need to write routing codes written in express docs
app.get("hellow/world", hellow.world); app.post("hellow/world", hellow.world); app.put("hellow/world", hellow.world); app.delete("hellow/world", hellow.world);
You only have to write easy code like bellow
require('express-dynamic-router') .index(require('routes/index').index) .register(app);
Instalation
npm install express-dynamic-router
Usage
express/app.js
app.configure('development', function(){ app.use(express.errorHandler()); }); //use express-dynamic-router require('express-dynamic-router') .index(require('routes/index').index) // Set action for index .register(app); http.createServer(app).listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });
If you change routes dir, You only have to add setRoutesDir(path-your-route-dir) on method chain
require('express-dynamic-router') .index(require('routes/index').index) .setRoutesDir("path-to-your-routes") .register(app);
Api Reference
register
Register all routes in your routes dir except for actions which are ignored.
dynamicRouter.register(object expressApp)
setRoutesDir
Set path to your routes dir (First, dynamic-router browse express/routes, if you don't use this method.)
dynamicRouter.setRoutesDir(string path-to-your-custome-routes-dir)
getRoutesDir
Get your routes dir string.
dynamicRouter.getRoutesDir()
index
Set index action (accessible http://your-domain.com/)
dynamicRouter.index(function indexAction)
applyOnly
Register only routes which are given arguments.
dynamicRouter.applyOnly(['routes1', '....'])
■usage
dynamicRouter .applyOnly(['user', 'account']) }) .register('app); # GET /user/index -> 200 OK # POST /account/save -> 201 Created # GET /mypage/index -> 404 Not Found
ignore
Set ignore actions which you not want to register.
dynamicRouter.ignore(object ignoreAction)
■usage
dynamicRouter .ignore({ '/*' : ['hogehoge', 'fugafuga'], // apply ignoreing to all routes. 'user' : ['initialize', 'beforeRenderHook'] // apply ignoreing to user. }) .register('app); # GET /user/hogehoge -> 404 Not Found # POST /account/fugafuga -> 404 Not Found # GET /user/initialize -> 404 Not Found # GET /user/list -> 200 OK
Runing tests
mocha test/router.js
License
MIT