express-route-controller
This is a helper function to assign controller actions to routes in express (Not sure if it works with connect as well).
It provides just a tiny bit of structure on top of a normal express app, but also doesn't get in the way at all, and you can continue to use express normally and define even more routes manually if you wish.
Usage:
In your express project install express-route-controller:
npm install express-route-controller
Now create a folder where you want all your controllers, eg. controllers
, and add a file in there,
named mycontroller.js
. Then define it somehow, like so:
moduleexports = { } { };
In your main app.js file (or wherever you set up express routes normally) simply call the helper function (very sparse demo express app):
var express = ;var app = ;var erc = ; // set up express route control:; app;
You can make this even more easier, by defining your routes in a routes.json
file, like so:
And loading the routes is as simple as:
... ; ...
Feedback
Pull requests, feature ideas and bug reports are welcome
Todo
- Figuring out a way to deal with middleware, for non-gloabl type of middleware
- It would be nice to figure out a way to add unit tests
- Possibly there should be an easy mode for everybody who wants to use the defaults. Either that or having the basepath as a spearate variable and including a routes.json file if the routes argument passed is a string rather than an object
- It would be nice to have a command line utility to genearte the controllers. Possibly with a
res.send('name of action');
in the function body
License
MIT