@valbo/not-found-middleware

Express middleware that throws a not found error.

Usage no npm install needed!

<script type="module">
  import valboNotFoundMiddleware from 'https://cdn.skypack.dev/@valbo/not-found-middleware';
</script>

README

@valbo/not-found-middleware

Express middleware that throws a not found error.

npm (scoped) semantic-release Build Status Coverage Status Known Vulnerabilities

Install

npm install @valbo/not-found-middleware

Usage

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"
  }
}