Express middleware that ensures requests match the specified content-type.
$ npm install @moxy/express-ensure-content-type
const express = ;const bodyParser = ;const ensureContentType = ;const app = ;app;
Creates a middleware that validates a request content-type against
If the validation fails,
next will be called with an Error created with
string | array
One or more content-types to match against. It can be:
- A file extension name such as json. This name will be returned if matched.
- A mime type such as application/json.
- A mime type with a wildcard such as / or /json or application/. The full mime type will be returned if matched.
- A suffix such as +json. This can be combined with a wildcard such as /vnd+json or application/+json. The full mime type will be returned if matched.
type-is for more information.
When to validate. Can be set to:
always- Always validate.
body-present- Validate if body is present, that is, when the request has
body-not-empty- Same as
body-presentbut skips if
Content-Lengthis 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.