Created
October 4, 2011 13:20
-
-
Save indexzero/1261627 to your computer and use it in GitHub Desktop.
Simple example of loading routes from multiple modules in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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