Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

fastify-405

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-405

Add 405 HTTP status to your routes

latest
Source
npmnpm
Version
4.0.0
Version published
Weekly downloads
6
200%
Maintainers
0
Weekly downloads
 
Created
Source

fastify-405

ci npm JavaScript Style Guide

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

Install

npm install fastify-405

Compatible

Plugin versionFastify 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!

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:

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

Keywords

405

FAQs

Package last updated on 17 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts