Fix crash with express 5
express.json does not enforce json. this means it will pass it through but let req.body be undefined. this causes all our asserts to crash
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
exports = module.exports = {
|
||||
cookieParser: require('cookie-parser'),
|
||||
cors: require('./cors.js'),
|
||||
json: require('./json.js'),
|
||||
lastMile: require('connect-lastmile'),
|
||||
multipart: require('./multipart.js'),
|
||||
timeout: require('connect-timeout')
|
||||
|
||||
18
src/middleware/json.js
Normal file
18
src/middleware/json.js
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
|
||||
function _mime(req) {
|
||||
const str = req.headers['content-type'] || '';
|
||||
return str.split(';')[0];
|
||||
}
|
||||
|
||||
exports = module.exports = function (options) {
|
||||
const json = express.json(options);
|
||||
|
||||
return function (req, res, next) {
|
||||
// enforce json body type. without this the json middleware will skip parsing and req.body might be undefined
|
||||
if (_mime(req) !== 'application/json') return res.status(400).send('incorrect mime type. expecting application/json');
|
||||
json(req, res, next);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user