UNPKG

1.95 kBMarkdownView Raw
1# http-errors
2
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]
8
9Create HTTP errors for Express, Koa, Connect, etc. with ease.
10
11## Example
12
13```js
14var createError = require('http-errors');
15
16app.use(function (req, res, next) {
17 if (!req.user) return next(createError(401, 'Please login to view this page.'));
18 next();
19})
20```
21
22## API
23
24This is the current API, currently extracted from Koa and subject to change.
25
26### Error Properties
27
28- `message`
29- `status` and `statusCode` - the status code of the error, defaulting to `500`
30
31### createError([status], [message], [properties])
32
33```js
34var err = createError(404, 'This video does not exist!');
35```
36
37- `status: 500` - the status code as a number
38- `message` - the message of the error, defaulting to node's text for that status code.
39- `properties` - custom properties to attach to the object
40
41### new createError\[code || name\](\[msg]\))
42
43```js
44var err = new createError.NotFound();
45```
46
47- `code` - the status code as a number
48- `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.
49
50## License
51
52[MIT](LICENSE)
53
54[npm-image]: https://img.shields.io/npm/v/http-errors.svg?style=flat
55[npm-url]: https://npmjs.org/package/http-errors
56[node-version-image]: https://img.shields.io/node/v/http-errors.svg?style=flat
57[node-version-url]: http://nodejs.org/download/
58[travis-image]: https://img.shields.io/travis/jshttp/http-errors.svg?style=flat
59[travis-url]: https://travis-ci.org/jshttp/http-errors
60[coveralls-image]: https://img.shields.io/coveralls/jshttp/http-errors.svg?style=flat
61[coveralls-url]: https://coveralls.io/r/jshttp/http-errors
62[downloads-image]: https://img.shields.io/npm/dm/http-errors.svg?style=flat
63[downloads-url]: https://npmjs.org/package/http-errors