Hapi-405-Routes
Plugin for Hapi.js 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:
GET /farm/goats
-- route which retrieves data about all goats on the farm.
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 already implemented routes which respond with a 405 status code. Using this plugin and requesting OPTIONS /farm/goats
will respond with a 405 Method Not Allowed.
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.
server.register([
// other service plugins register before routes
]).then(() => {
// my service routes
server.route(routes);
}).then(() => {
// this 405 route plugin
server.register([
{
register: require('hapi-405-routes'),
options: {
methodsToSupport: ['GET', 'DELETE', 'PATCH', 'POST', 'OPTIONS'],
log: true,
routePrefix: 'farm'
}
}
]);
});
Options
This plugin supports 3 options passed in during plugin registration: methodsToSupport
, routePrefix
, and log
.
Option Name | Data Type | Defaults | Description |
---|
methodsToSupport | Array<String> | ['get', 'post', 'delete', 'put', 'patch', 'options', 'trace'] | This option specifies which methods should respond with a 405 status code: "Method Not Allowed" if not alredy implemented. |
routePrefix | String | '' | Specifies the prefix used to generate the routes existing routes. Must provide if your routes are registered with using a prefix. |
log | Boolean | false | Enables plugin logging |