Skip to content
Add 405 HTTP status to your routes
Branch: master
Clone or download
Eomm Merge pull request #2 from Eomm/ci
add travis ci config and badge
Latest commit 0dd2651 Mar 2, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.gitignore
.travis.yml add travis ci config and badge Mar 2, 2019
LICENSE add: first version of plugin 405 Mar 1, 2019
README.md
package.json add travis ci config and badge Mar 2, 2019
plugin.js

README.md

fastify-405

Coverage Status Build Status JavaScript Style Guide

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

Install

npm install fastify-405

This plugin support Node.js >=6 and Fastify ^2

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!

const fastify = require('fastify')()

fastify.register(require('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' })
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log('Server listening at http://localhost:3000')
})

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
fastify.register(require('fastify-rate-limit'), {
  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, Licensed under MIT.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.