Create HTTP error objects
npm install http-errors-lite
[![NPM Downloads][npm-downloads-image]][node-url]
[![Node.js Version][node-image]][node-url]

Create HTTP errors for Express, Koa, Connect, etc. with ease. This is a drop-in replacement for the normal http-errors only with zero dependancies and 50% less memory usage (give or take).
This is a Node.js module available through the
npm registry. Installation is done using thenpm install command:
``bash`
$ npm install http-errors-lite
`js
var createError = require('http-errors-lite')
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.
- expose - can be used to signal if message should be sent to the client,false
defaulting to when status >= 500headers
- - can be an object of header names to values to be sent to theundefined
client, defaulting to . When defined, the key names should allmessage
be lower-cased
- - the traditional error message, which should be kept short and allstatus
single line
- - the status code of the error, mirroring statusCode for generalstatusCode
compatibility
- - the status code of the error, defaulting to 500
Create a new error object with the given message msg.createError.HttpError
The error object inherits from .
`js`
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
Extend the given error object with createError.HttpErrorerror
properties. This will not alter the inheritance of the given object, and the modified error object is the
return value.
`js`
fs.readFile('foo.txt', function (err, buf) {
if (err) {
if (err.code === 'ENOENT') {
var httpError = createError(404, err, { expose: false })
} else {
var httpError = createError(500, err)
}
}
})
- status - the status code as a numbererror
- - the error object to extendproperties` - custom properties to attach to the object
-
[node-image]: https://badgen.net/npm/node/http-errors-lite
[node-url]: https://nodejs.org/en/download
[npm-downloads-image]: https://badgen.net/npm/dm/http-errors-lite
[npm-url]: https://npmjs.org/package/http-errors-lite
[travis-image]: https://travis-ci.org/nfp-projects/http-errors-lite.svg?branch=master
[travis-url]: https://travis-ci.org/nfp-projects/http-errors-lite