UNPKG

hapi-405-routes

Version:

Allows 405 'Method Not Allowed' responses for hapi routes

76 lines (55 loc) 2.75 kB
# Hapi-405-Routes [![Build Status](https://travis-ci.org/RiptideElements/hapi-405-routes.svg?branch=master)](https://travis-ci.org/RiptideElements/hapi-405-routes) Plugin for [Hapi.js](http://hapijs.com/) to include 405 Method Not Allowed Responses on routes for a given set of methods. ## Overview This plugin registers additional routes with your hapi service for routes with specific methods not implemented. Say you have a farming web service has an api with only 2 routes: ```js GET /farm/goats // route which retrieves data about all goats on the farm. ``` ```js POST /farm/goats // route which allows for creation of additional goats. ``` By default, hapi responds with a 404 if there is not route/method match registered with the service. If a user were to request `OPTIONS /farm/goats` they would get a 404 response. This plugin builds additional routes based on the route paths already implemented which will respond with a 405 status code. Using this plugin and requesting `OPTIONS /farm/goats` will respond with a 405 Method Not Allowed. Additionally the 405 routes can be configured to respond with an `allow` header specifying which methods are allowed for the requested route path. See [options](# Options) below. ### Installing This plugin is available through an npm module. ``` npm install hapi-405-routes ``` ## Usage This plugin must be registered **after** the implemented routes have already been registered with the service. ```js // my service routes server.route(routes); // this 405 route plugin server.register([ { register: require('hapi-405-routes'), options: { methodsToSupport: ['GET', 'DELETE', 'PATCH', 'POST', 'OPTIONS'], setAllowHeader: true, log: true } } ]); ``` ## Options This plugin supports 4 options passed in during plugin registration: `methodsToSupport`, `setAllowHeader`, `allowHeadWithGet`, and `log`. #### methodsToSupport * **Data Type**: `Array<String>` * **Defaults**: `['GET', 'POST', 'DELETE', 'PUT', 'PATCH', 'OPTIONS', 'TRACE']` * **Description**: This option specifies which methods should respond with a 405 status code: "Method Not Allowed" if not already implemented. #### log * **Data Type**: `Boolean` * **Defaults**: `false` * **Description**: Enables plugin logging #### setAllowHeader * **Data Type**: `Boolean` * **Defaults**: `false` * **Description**: Sets an `allow` header with each 405 response containing the methods implemented for the related route path. #### allowHeadWithGet * **Data Type**: `Boolean` * **Defaults**: `false` * **Description**: Includes `HEAD` with the allow header if a `GET` method is implemented for the related route path. (Hapi does not natively support `HEAD` methods)