@mark01/express-utils
Version:
npm package that contains utilities for express.js
16 lines (15 loc) • 658 B
JavaScript
import { json as parse } from 'express';
import { AppError } from '../error';
export const bodyParser = (req, res, next) => {
const { ['content-type']: type, ['content-length']: length } = req.headers;
if (!type?.includes('application/json')) {
next(new AppError("This API only accepts requests with 'Content-Type' set to 'application/json'.", 415));
return;
}
if (length && Number.parseInt(length, 10) > 512) {
next(new AppError('Request is too large! Please reduce your payload.', 413));
return;
}
return parse({ type: 'application/json', limit: 512 })(req, res, next);
};
export default bodyParser;