Skip to content

valverdealbo/not-found-middleware

Repository files navigation

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

About

Express middleware that throws a not found error

Resources

Stars

Watchers

Forks

Packages

No packages published