I read from various posts here that a good way to structure an Express app is to create a routes.js module with contents such as:
exports.homepage = function (req, res) {
// do something
}
Then, from my app.js:
var app = module.exports = express.createServer();
app.get('/', routes.homepage);
This works a treat until I want to change my "do something" to do another request like so:
exports.homepage = function (req, res) {
app.get('/sign-in', myCallbackFunc);
}
My routes.js knows nothing about app. How do I pass in/reference "app". Or is the way I've structured this incorrect?
Thanks!
Not sure, how your project is structured. But this, is how I would create the routes.js file:
var routes = function(app) {
app.get('/', function(req, res) {
// do something
});
app.get('/sign-in', function(req, res) {
// do something
});
};
module.exports = routes;
And the from your app.js, simply require your routes file like so:
require('./routes')(app);