An idea for defining routes for Express applications in Objects of paths, (optional) verbs, then handlers:
var express = require('express');
var routing = require('./routing');
var app = express();
routing(app, {
// `get` only
'/': function (req, res) {
res.render('index');
},
'/contact': {
get: function (req, res) {
res.render('contact');
},
post: function (req, res) {
// ...
}
}
});
They can also be defined and found in modules under a basedir
via glob. This does assume the basedir
is dedicated to such modules, however:
// ...
routing(app, __dirname + '/routes');
// ./routes/index.js
module.exports = {
'/': function (req, res) {
res.render('index');
}
};
// ./routes/contact.js
module.exports = {
'/contact': {
get: function (req, res) {
res.render('contact');
},
post: function (req, res) {
// ...
}
}
};
Modules are sorted by their dirname
, then by their basename
with priority given to index.js
.