3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Create HTTP errors for Express, Koa, Connect, etc. with ease.
13 This is a [Node.js](https://nodejs.org/en/) module available through the
14 [npm registry](https://www.npmjs.com/). Installation is done using the
15 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
18 $ npm install http-errors
24 var createError = require('http-errors')
25 var express = require('express')
28 app.use(function (req, res, next) {
29 if (!req.user) return next(createError(401, 'Please login to view this page.'))
36 This is the current API, currently extracted from Koa and subject to change.
38 All errors inherit from JavaScript `Error` and the exported `createError.HttpError`.
42 - `expose` - can be used to signal if `message` should be sent to the client,
43 defaulting to `false` when `status` >= 500
44 - `headers` - can be an object of header names to values to be sent to the
45 client, defaulting to `undefined`. When defined, the key names should all
47 - `message` - the traditional error message, which should be kept short and all
49 - `status` - the status code of the error, mirroring `statusCode` for general
51 - `statusCode` - the status code of the error, defaulting to `500`
53 ### createError([status], [message], [properties])
55 <!-- eslint-disable no-undef, no-unused-vars -->
58 var err = createError(404, 'This video does not exist!')
61 - `status: 500` - the status code as a number
62 - `message` - the message of the error, defaulting to node's text for that status code.
63 - `properties` - custom properties to attach to the object
65 ### new createError\[code || name\](\[msg]\))
67 <!-- eslint-disable no-undef, no-unused-vars -->
70 var err = new createError.NotFound()
73 - `code` - the status code as a number
74 - `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.
76 #### List of all constructors
78 |Status Code|Constructor Name |
79 |-----------|-----------------------------|
82 |402 |PaymentRequired |
85 |405 |MethodNotAllowed |
87 |407 |ProxyAuthenticationRequired |
88 |408 |RequestTimeout |
91 |411 |LengthRequired |
92 |412 |PreconditionFailed |
93 |413 |PayloadTooLarge |
95 |415 |UnsupportedMediaType |
96 |416 |RangeNotSatisfiable |
97 |417 |ExpectationFailed |
99 |421 |MisdirectedRequest |
100 |422 |UnprocessableEntity |
102 |424 |FailedDependency |
103 |425 |UnorderedCollection |
104 |426 |UpgradeRequired |
105 |428 |PreconditionRequired |
106 |429 |TooManyRequests |
107 |431 |RequestHeaderFieldsTooLarge |
108 |451 |UnavailableForLegalReasons |
109 |500 |InternalServerError |
110 |501 |NotImplemented |
112 |503 |ServiceUnavailable |
113 |504 |GatewayTimeout |
114 |505 |HTTPVersionNotSupported |
115 |506 |VariantAlsoNegotiates |
116 |507 |InsufficientStorage |
118 |509 |BandwidthLimitExceeded |
120 |511 |NetworkAuthenticationRequired|
126 [npm-image]: https://img.shields.io/npm/v/http-errors.svg
127 [npm-url]: https://npmjs.org/package/http-errors
128 [node-version-image]: https://img.shields.io/node/v/http-errors.svg
129 [node-version-url]: https://nodejs.org/en/download/
130 [travis-image]: https://img.shields.io/travis/jshttp/http-errors.svg
131 [travis-url]: https://travis-ci.org/jshttp/http-errors
132 [coveralls-image]: https://img.shields.io/coveralls/jshttp/http-errors.svg
133 [coveralls-url]: https://coveralls.io/r/jshttp/http-errors
134 [downloads-image]: https://img.shields.io/npm/dm/http-errors.svg
135 [downloads-url]: https://npmjs.org/package/http-errors