Express middleware that ensures requests match the specified content-type.
Express middleware that ensures requests match the specified content-type.
$ npm install @moxy/express-ensure-content-type
const express = require('express');
const bodyParser = require('body-parser');
const ensureContentType = require('@moxy/express-ensure-content-type');
const app = express();
app.post(
'/',
bodyParser(),
ensureContentType('application/json'),
(req, res, next) => {
console.log(req.body);
},
);
Creates a middleware that validates a request content-type against contentType
.
If the validation fails, next
will be called with an Error created with http-errors
.
Type: string | array
One or more content-types to match against. It can be:
Please check type-is
for more information.
Type: object
Type: string
Default: always
When to validate. Can be set to:
always
- Always validate.body-present
- Validate if body is present, that is, when the request has Transfer-Encoding
or Content-Length
headers.body-not-empty
- Same as body-present
but skips if Content-Length
is explicitly set to 0, as set by some HTTP clients unnecessarily.Any parameter passed to the test
command is passed down to Jest.
$ npm t
$ npm t -- --watch # To run watch mode
Released under the MIT License.