Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable/remove a middleware for specific route in ExpressJS?

I want to disable a specific middleware, which I've set up previously in the app.js, for example:

app.use(express.bodyParser());

And then I want to remove that bodyParser() for a specific route for instance:

app.post("/posts/add", Post.addPost);

Thank you

like image 571
mrblue Avatar asked Oct 12 '13 18:10

mrblue


1 Answers

You could write a function to detect a condition, like this:

function maybe(fn) {
    return function(req, res, next) {
        if (req.path === '/posts/add' && req.method === 'POST') {
            next();
        } else {
            fn(req, res, next);
        }
    }
}

And then modify the app.use statement:

app.use(maybe(express.bodyParser()));
like image 147
Werner Kvalem Vesterås Avatar answered Sep 18 '22 18:09

Werner Kvalem Vesterås