express-allow-methods
Version:
Express middleware for sending HTTP 405 Method Not Allowed response
56 lines (40 loc) • 1.08 kB
Markdown
Express middleware for sending [`HTTP 405 Method Not Allowed`](https://httpstatuses.com/405) response. Due to the specification requirements, it also responds with the `Allow` header with comma-separated list of allowed methods.
<center>
<img src="demo.gif" alt="Package demo using cUrl terminal command" />
</center>
- Node.JS v0.11.8+
- (optionally) TypeScript v3.8.2+
```sh
npm i express-allow-methods
npm i --save-dev typescript
```
```ts
import allowMethods from "express-allow-methods";
```
```ts
const allowMethods = require("express-allow-methods").default;
```
```ts
import express from "express";
import allowMethods from "express-allow-methods";
const app = express();
app.route("/")
.all(allowMethods("GET", "POST"))
.get((req, res) => {
// GET /
})
.post((req, res) => {
// POST /
})
.patch((req, res) => {
// PATCH /
// the code here will never run
});
```