Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 >=10 and Fastify ^3.2.x Checkout the v1 for Fastify v2.

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-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, Licensed under MIT.