Socket
Book a DemoInstallSign in
Socket

@moxy/express-ensure-content-type

Package Overview
Dependencies
Maintainers
22
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@moxy/express-ensure-content-type

Express middleware that ensures requests match the specified content-type

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
0
Maintainers
22
Weekly downloads
 
Created
Source

express-ensure-content-type

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

Express middleware that ensures requests match the specified content-type.

Installation

$ npm install @moxy/express-ensure-content-type

Usage

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);
    },
);

API

ensureContentType(contentType, when?)

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.

contentType

Type: 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.

Please check type-is for more information.

options?

Type: object

options.when

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.

Tests

Any parameter passed to the test command is passed down to Jest.

$ npm t
$ npm t -- --watch  # To run watch mode

License

Released under the MIT License.

Keywords

express

FAQs

Package last updated on 08 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts