# fastify-405

> Add 405 HTTP status to your routes

Latest version **4.0.1** (published 2026-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-405
pnpm add fastify-405
yarn add fastify-405
bun add fastify-405
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2026-10-18 |
| First published | 2019-03-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 7 |
| Author | Manuel Spigolon |
| Maintainers | eomm |
| Keywords | 405, fastify-plugin, http-status |

## Links

- npm: https://www.npmjs.com/package/fastify-405
- Repository: https://github.com/Eomm/fastify-405
- Homepage: https://github.com/Eomm/fastify-405#readme
- Issues: https://github.com/Eomm/fastify-405/issues
- Funding: https://github.com/Eomm/fastify-405?sponsor=1
- npm.io page: https://npm.io/package/fastify-405

## Dependencies (1)

- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^5.0.0

## Recent versions

- 4.0.1 (latest) — 2026-10-18
- 4.0.0 — 2024-11-17
- 3.2.0 — 2022-10-12
- 3.1.0 — 2022-10-07
- 3.0.0 — 2022-05-31
- 2.0.0 — 2020-08-06
- 1.0.1 — 2019-03-28
- 1.0.0 — 2019-03-01

## README

# fastify-405

[![ci](https://github.com/Eomm/fastify-405/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Eomm/fastify-405/actions/workflows/ci.yml)
[![npm](https://img.shields.io/npm/v/fastify-405)](https://www.npmjs.com/package/fastify-405)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Add 405 Method Not Allowed HTTP status to your routes, instead of the default 404.


## Install

```
npm install fastify-405
```

### Compatible

| Plugin version | Fastify version |
| ------------- |:---------------:|
| `^4.0.0` | `^5.0.0` |
| `^3.0.0` | `^4.0.0` |
| `^2.0.0` | `^3.2.0` |
| `^1.0.0` | `^2.0.0` |


## Usage

Register the plugin with some custom option.
It will add an `onRoute` hook and will add an handler
that replay with HTTP status 405 and the `allow` response header.

The `allow` header will contains what you define in the options.

This plugin has been tested also with the encapsulation!

```js
import Fastify from 'fastify'

const fastify = Fastify()
await fastify.register(import('fastify-405'), {
  regexp: /\/foo.*/,
  allow: ['GET', 'HEAD']
})

// This route will reply 405 on POST, HEAD, OPTIONS, PUT..
fastify.get('/foo', (req, reply) => {
  reply.send({ hello: 'world' })
})

// This route will not match the fastify-405 regexp and will reply with 404 on other HTTP methods
fastify.get('/bar', (req, reply) => {
  reply.send({ hello: 'world' })
})

await fastify.listen({ port: 3000 })
console.log('Server listening at http://localhost:3000')
```

> **Note**
> You need to `await` the plugin registration to make sure the plugin is ready to use.
> All the routes defined **before** the plugin registration will be ignored.
> This change has been introduced in Fastify v4.

### Options

You can pass the following options during the registration:

| Option | Default | Description |
|--------|---------|-------------|
|`regexp`| `/.*/`  | The regular expression the route must fulfil in order to add the 405 handler
|`allow` | `['GET', 'POST']` | The method that the route will allow, the HTTP methods that are not in this array will reply 405

```js
await fastify.register(require('fastify-405'), {
  regexp: /\/foo.*/, // must be a regular expression
  allow: ['GET', 'POST'] // could be only a subset of: ['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'PATCH']
})
```


## License

Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).

---
_Source: https://npm.io/package/fastify-405 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
