https://github.com/eomm/fastify-405
Add 405 HTTP status to your routes
https://github.com/eomm/fastify-405
405 fastify fastify-plugin hacktoberfest http-status-code
Last synced: 7 days ago
JSON representation
Add 405 HTTP status to your routes
- Host: GitHub
- URL: https://github.com/eomm/fastify-405
- Owner: Eomm
- License: mit
- Created: 2019-03-01T17:43:31.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-02-24T17:37:59.000Z (3 months ago)
- Last Synced: 2025-05-06T21:07:38.822Z (7 days ago)
- Topics: 405, fastify, fastify-plugin, hacktoberfest, http-status-code
- Language: JavaScript
- Homepage:
- Size: 42 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-405
[](https://github.com/Eomm/fastify-405/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/fastify-405)
[](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).