Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created October 4, 2011 13:20
Show Gist options
  • Save indexzero/1261627 to your computer and use it in GitHub Desktop.
Save indexzero/1261627 to your computer and use it in GitHub Desktop.
Simple example of loading routes from multiple modules in node.js
var fs = require('fs'),
path = require('path'),
journey = require('journey');
exports.loadAllRoutes = function () {
var router = new (journey.Router)({
strict: false,
strictUrls: false,
api: 'basic'
});
var modulesDir = path.join(__dirname, 'lib', 'myapp');
fs.readdirSync(modulesDir).forEach(function (dir) {
try {
//
// Attempt to require the path to load it into memory
//
dir = path.join(modulesDir, dir);
var mod = require(dir);
if (mod && mod.addRoutes) {
mod.addRoutes()(router);
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment