I'm trying to debug some seemingly random API failures in an API built with express version 4.14.0 and I'm confused about a few things. Here is the relevant code:app.use(bodyParser.json({ limit: '100mb' })); app.use(bodyParser.urlencoded({ extended: false })); app.use((err, req, res, next) => { res.sendError(new CustomError( 'Request could not be parsed. Double check your data as well as your headers (e.g. Content-Type)', 400, err )); }); app.use(routes); This is what I would expect to happen: All API requests are rejected. That code is loading some middleware before it loads the routes and that middleware never calls next() and simply always returns that 400 error. Thus, I'd expect the entire API to be completely broken.But the API works fine. The error is somehow only triggered when you send it malformed JSON (or presumably a bad content-type). I assume this is somehow tied in with the "body-parser" package and the validation it's doing but I don't understand how the error can be tied to the results of the bodyParser validation when the 2 pieces of code seem to be loaded as completely unrelated middleware.The bug I'm trying to track down is why that 400 error is returned sometimes for valid requests, but I'm stumped as to how this is even working at all. Any help would be much appreciated.
Submitted April 02, 2019 at 03:39PM by doctorlongghost
No comments:
Post a Comment