Restrict express.json() on certain URLs [duplicate]
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
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()));