I have an NodeJS Express app that is getting really big in just one file (app.js).
I want to export all my routes into a single, external file, say ./lib/routes.js. How to do that?
How to export the following bit of code and require it correctly in main app.js?
app.get('/logout', function(req, res) {
res.render('logout', {
username: req.session.username
});
});
app.get('/dashboard', function(req, res) {
res.render('dashboard', {
username: req.session.username
});
});
app.get('/login', function(req, res) {
res.render('login', {
badLogin: false,
loginError: false
});
});
Why not do something like this:
Also, you could imagine easily using http://nodejs.org/docs/v0.4.8/api/fs.html#fs.readdir to loop through a routes directory and load these up programmatically.
You could even do something along the lines of…
Though I think I’d spend a little time thinking about how to simplify that.