What I am doing is grouping my routes with a controller. For each group of related routes (users, shopping carts, whatever), I create a controller file that lives in app/controllers/foo.js
, where foo
is the name of the controller. In my main javascript server file (where all your code currently lives), I require
each controller by name and then call its setup
function, passing in my express app
object and letting the controller add all the routes needed.
['api', 'authorization', 'users', 'tests'].map(function(controllerName) { var controller = require('./controllers/' + controllerName); controller.setup(app); });
Inside each controller, I am doing something like:
exports.setup = function(app) { app.get('/dashboard', function(req, res) { res.render('dashboard', { username: req.session.username }); }); };
Peter Lyons
source share