am

allow-methods

Express/connect middleware to handle 405 errors

Showing:

Popularity

Downloads/wk

450

GitHub Stars

2

Maintenance

Last Commit

2yrs ago

Contributors

2

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Allow-Methods

Express/connect middleware to handle 405 errors, when a request method is not supported by your route or application.

NPM version Node.js version support Build status Code coverage Dependencies MIT licensed

Install

Install Allow-Methods with npm:

npm install allow-methods

Usage

var allowMethods = require('allow-methods');

allowMethods will return a middleware function that will error if the request method does not match one of the allowed methods. The error will have message and status properties which you can use.

It accepts two arguments. Firstly, an array of allowed methods:

allowMethods(['get', 'head', 'post']);

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

allowMethods(['get', 'head'], 'Unsupported method');

Route-level

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

app
    .route('/example')
    // Only requests with a GET/HEAD method will continue
    .all(allowMethods(['get', 'head']))
    // Define GET handler
    .get(function () { ... });

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(function (err, req, res, next) {
    res.status(err.status || 500).send({
        message: err.message
    });
});

Application-level

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

// Only allow GET/HEAD methods across the entire application
app.use(allowMethods(['get', 'head']));

Contributing

To contribute to Allow-Methods, clone this repo locally and commit your code on a separate branch.

Please write unit tests for your code, and check that everything works by running the following before opening a pull-request:

make ci

Support and Migration

Allow Methods major versions are normally supported for 6 months after their last minor release. This means that patch-level changes will be added and bugs will be fixed. The table below outlines the end-of-support dates for major versions, and the last minor release for that version.

We also maintain a migration guide to help you migrate.

Major VersionLast Minor ReleaseNode.js VersionsSupport End Date
❤️2N/A4+N/A
11.00.10–72018-04-16

If you're opening issues related to these, please mention the version that the issue relates to.

License

Allow-Methods is licensed under the MIT license.
Copyright © 2015, Rowan Manning

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial