UNPKG

3.56 kBMarkdownView Raw
1# statuses
2
3[![NPM Version][npm-version-image]][npm-url]
4[![NPM Downloads][npm-downloads-image]][npm-url]
5[![Node.js Version][node-version-image]][node-version-url]
6[![Build Status][ci-image]][ci-url]
7[![Test Coverage][coveralls-image]][coveralls-url]
8
9HTTP status utility for node.
10
11This module provides a list of status codes and messages sourced from
12a few different projects:
13
14 * The [IANA Status Code Registry](https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml)
15 * The [Node.js project](https://nodejs.org/)
16 * The [NGINX project](https://www.nginx.com/)
17 * The [Apache HTTP Server project](https://httpd.apache.org/)
18
19## Installation
20
21This is a [Node.js](https://nodejs.org/en/) module available through the
22[npm registry](https://www.npmjs.com/). Installation is done using the
23[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
24
25```sh
26$ npm install statuses
27```
28
29## API
30
31<!-- eslint-disable no-unused-vars -->
32
33```js
34var status = require('statuses')
35```
36
37### status(code)
38
39Returns the status message string for a known HTTP status code. The code
40may be a number or a string. An error is thrown for an unknown status code.
41
42<!-- eslint-disable no-undef -->
43
44```js
45status(403) // => 'Forbidden'
46status('403') // => 'Forbidden'
47status(306) // throws
48```
49
50### status(msg)
51
52Returns the numeric status code for a known HTTP status message. The message
53is case-insensitive. An error is thrown for an unknown status message.
54
55<!-- eslint-disable no-undef -->
56
57```js
58status('forbidden') // => 403
59status('Forbidden') // => 403
60status('foo') // throws
61```
62
63### status.codes
64
65Returns an array of all the status codes as `Integer`s.
66
67### status.code[msg]
68
69Returns the numeric status code for a known status message (in lower-case),
70otherwise `undefined`.
71
72<!-- eslint-disable no-undef, no-unused-expressions -->
73
74```js
75status['not found'] // => 404
76```
77
78### status.empty[code]
79
80Returns `true` if a status code expects an empty body.
81
82<!-- eslint-disable no-undef, no-unused-expressions -->
83
84```js
85status.empty[200] // => undefined
86status.empty[204] // => true
87status.empty[304] // => true
88```
89
90### status.message[code]
91
92Returns the string message for a known numeric status code, otherwise
93`undefined`. This object is the same format as the
94[Node.js http module `http.STATUS_CODES`](https://nodejs.org/dist/latest/docs/api/http.html#http_http_status_codes).
95
96<!-- eslint-disable no-undef, no-unused-expressions -->
97
98```js
99status.message[404] // => 'Not Found'
100```
101
102### status.redirect[code]
103
104Returns `true` if a status code is a valid redirect status.
105
106<!-- eslint-disable no-undef, no-unused-expressions -->
107
108```js
109status.redirect[200] // => undefined
110status.redirect[301] // => true
111```
112
113### status.retry[code]
114
115Returns `true` if you should retry the rest.
116
117<!-- eslint-disable no-undef, no-unused-expressions -->
118
119```js
120status.retry[501] // => undefined
121status.retry[503] // => true
122```
123
124## License
125
126[MIT](LICENSE)
127
128[ci-image]: https://badgen.net/github/checks/jshttp/statuses/master?label=ci
129[ci-url]: https://github.com/jshttp/statuses/actions?query=workflow%3Aci
130[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/statuses/master
131[coveralls-url]: https://coveralls.io/r/jshttp/statuses?branch=master
132[node-version-image]: https://badgen.net/npm/node/statuses
133[node-version-url]: https://nodejs.org/en/download
134[npm-downloads-image]: https://badgen.net/npm/dm/statuses
135[npm-url]: https://npmjs.org/package/statuses
136[npm-version-image]: https://badgen.net/npm/v/statuses