Express implHandler
Simple express method implementation script to catch error code 501
This script is useful if you're developing an API with Express or you'd like to set a default 501 error for your actual server endpoints unused verbs.
const implHandler =
Example of use
Put this inside your API router just before the 404 error catcher.
Root API endpoint (/api/v1
) router example:
const express = const router = expressconst implHandler = const users = const cars = // define your routesrouter routerrouter // catch 501 and forward to error handler // catch 404 and forward to error handler// ... // JSON error middleware moduleexports = router
Then if you try to call to your API endpoints with some method not defined you will receive a 501 response code. For example if you try to make a request PUT /api/v1/
the server will return a 501
HTTP code with the next content:
Arguments
app
or router
instance
Your router instance (router
) or your app instance (app
).
Note: Pay attention that before call this script the router must have the endpoints registered in order to handle them.
middleware
(optional)
Default:*
You can override the default middleware with one of your preference:
Collaborate
Feel free to make pull request or raise issues!
License
MIT