Reputation: 47
let shouldParseRequest = function (req) {
let url = req.originalUrl;
return (url.startsWith('/api/payments/stripe-webhook') || url.startsWith('/uploadimg'));
}
let parseJSON = bodyParser.json({ limit: '900kb' });
let urlencoded = bodyParser.urlencoded({extended: true})
app.use((req, res, next) => shouldParseRequest(req) ? parseJSON(req, res, next) : next())
So I had a question, how would I make the bodyParser json and urlencoded only run whenever the URLs don't include the ones above. I got this code from a github issue but I can't seem to make both of them run. Is there a better way to do this or how can I fix my current code?
Here is the github issue where I got the code from: https://github.com/expressjs/body-parser/issues/245
Upvotes: 1
Views: 1774
Reputation: 2365
Remove the below line in your code, if you have used it.
app.use(bodyParser.json());
and use body-parser middleware on routes wherever it is needed like below code:
app.post('/', bodyParser.json(), (req, res) => {
//...your code
})
If it is not needed in some routes you may skip it like below:
app.post('/url', (req, res) => {
//...your code
});
Upvotes: 2