I want to do a filter like rails before filter on express.js. I have a file named photo.js where I\'m putting all my photo related routes on there. But I need to redirect user
If you want to keep everything in your photo.js file, I think a better approach is to use app.all and pass multiple callbacks (which work like middleware in routing) built into the app routing. For instance
app.all('/photo/*', requireAuthentication, loadUser);
app.get('/photo/view', function(req, res) {
res.render('photo_view');
});
app.get('/photo/list', function(req, res) {
res.render('photo_list');
});
Where requireAuthentication
and loadUser
are functions.
Take a look the documentation for app.VERB and app.all at http://expressjs.com/api.html#app.all