The trick is that the app is local to the file that creates it. Thus, you should get this object in the area of ββother files.
Every other file must export funciton through which you can pass your application instance so that it can register new routes. This approach should work.
// home.js exports.register = function(app) { app.all('/', function(request, response) { ... }); }; // page1.js exports.register = function(app) { app.all('/page1', function(request, response) { ... }); }; // page2.js exports.register = function(app) { app.all('/page2', function(request, response) { ... }); }; //server.js - setup the app app = express.createServer(); require('./home').register(app); require('./page1').register(app); require('./page2').register(app);
And for the second part of your question, do you want to share some tuning methods?
app.all('*', function(req, res, next) { res.header 'x-snazzy-header', 'Im so snazzy' next() }); app.all('/page/:id', function(req, res) { res.send('content for page #'+ req.params('id')); });
First you can use * or named parameters like /users/:id to match the range of routes. And if you want to make some general settings, you can actually follow 2 routes. The route handler accepts the optional third argument next . When called, he will try to find the next route to match. This way you can customize things like generic headers for a bunch of routes.
source share