New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

require-header

Package Overview
Dependencies
Maintainers
0
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-header

Express/connect middleware to handle errors where an expected header is missing

  • 5.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Require-Header

Express middleware to handle errors where an expected header is missing.

Table of Contents

  • Requirements
  • Usage
  • Contributing
  • License

Requirements

This library requires the following to run:

Usage

Install with npm:

npm install allow-methods

Load the library into your code with a require call:

const requireHeader = require('require-header');

requireHeader will return a middleware function that will error if the request does not set the given header. The error will have message and status properties which you can use.

It accepts two arguments. Firstly, the name of a header which is required:

requireHeader('User-Agent');

Secondly (optionally) a message which will be used in the error if the request message does not match:

requireHeader('User-Agent', 'User-Agent header is required');

Route-level

const express = require('express');
const app = express();

// Only requests with a User-Agent header will continue
app.get(requireHeader('User-Agent'), () => { ... });

If you want to do something useful with the error, for example output a sensible JSON response, you will need to define an error handler for your application (after the route definition):

app.use((error, request, response, next) => {
    response.status(error.status || 500).send({
        message: error.message
    });
});

Application-level

const express = require('express');
const app = express();

// Require a User-Agent header across the entire application
app.use(requireHeader('User-Agent'));

Contributing

The contributing guide is available here. All contributors must follow this library's code of conduct.

License

Licensed under the MIT license.
Copyright © 2015, Rowan Manning

Keywords

FAQs

Package last updated on 27 Jul 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc