Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Filters on express.js

Filters on express.js


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 that is not authenticated on my system to the login page. I want to do an beforeFilter so then I dont need to put that logic in all my routes...


Problem courtesy of: rizidoro


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) {

app.get('/photo/list', function(req, res) {

Where requireAuthentication and loadUser are functions.

Take a look the documentation for app.VERB and app.all at

Solution courtesy of: Dave Jensen


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Filters on express.js


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription