Create HTTP errors for Express, Koa, Connect, etc. with ease.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
$ npm install http-errorsvar createError = require('http-errors')
var express = require('express')
var app = express()
app.use(function (req, res, next) {
if (!req.user) return next(createError(401, 'Please login to view this page.'))
next()
})This is the current API, currently extracted from Koa and subject to change.
All errors inherit from JavaScript Error and the exported createError.HttpError.
expose- can be used to signal ifmessageshould be sent to the client, defaulting tofalsewhenstatus>= 500headers- can be an object of header names to values to be sent to the client, defaulting toundefined. When defined, the key names should all be lower-casedmessage- the traditional error message, which should be kept short and all single linestatus- the status code of the error, mirroringstatusCodefor general compatibilitystatusCode- the status code of the error, defaulting to500
var err = createError(404, 'This video does not exist!')status: 500- the status code as a numbermessage- the message of the error, defaulting to node's text for that status code.properties- custom properties to attach to the object
var err = new createError.NotFound()code- the status code as a numbername- the name of the error as a "bumpy case", i.e.NotFoundorInternalServerError.
