@rowanmanning/not-found

Create Express middleware which results in an HTTP 404 Not Found error

Usage no npm install needed!

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

README

@rowanmanning/not-found

Create Express middleware which results in an HTTP 404 Not Found error.

Table of Contents

Requirements

This library requires the following to run:

Usage

Install with npm:

npm install @rowanmanning/not-found

Load the library into your code with a require call:

const notFound = require('@rowanmanning/not-found');

Create middleware which results in an HTTP 404 error in Express:

const express = require('express');
const notFound = require('@rowanmanning/not-found');

const app = express();

app.use(notFound());

Handle the 404 error like you would any other error in Express:

app.use((error, request, response, next) => {
    response.status(error.status); // 404
    response.send(error.message); // Not Found
});

Customise the error message with an option:

app.use(notFound({
    message: 'This page does not exist'
}));

app.use((error, request, response, next) => {
    response.status(error.status); // 404
    response.send(error.message); // This page does not exist
});

Contributing

The contributing guide is available here. All contributors must follow this library's code of conduct.

License

Licensed under the MIT license.
Copyright © 2019, Rowan Manning