Express middleware that throws a not found error.
npm install @valbo/not-found-middleware
This middleware sends a not found error via next() down the middleware chain. It is meant to be used as the last non-error middleware in the chain, to capture any url that has no router or middleware associated. It will include the request method and route in the message.
After this middleware there should be an error handler middleware that captures the error, like @valbo/error-handler-middleware.
import { notFoundMiddleware } from '@valbo/not-found-middleware';
import { errorHandlerMiddleware } from '@valbo/error-handler-middleware';
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
If a request is made to a route with no router or middleware associated, express would return:
{
"error": {
"status": 404,
"name": "NotFoundError",
"message": "POST /data not found"
}
}