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 |
|
---|
9 | HTTP status utility for node.
|
---|
10 |
|
---|
11 | This module provides a list of status codes and messages sourced from
|
---|
12 | a 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 |
|
---|
21 | This 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
|
---|
34 | var status = require('statuses')
|
---|
35 | ```
|
---|
36 |
|
---|
37 | ### status(code)
|
---|
38 |
|
---|
39 | Returns the status message string for a known HTTP status code. The code
|
---|
40 | may be a number or a string. An error is thrown for an unknown status code.
|
---|
41 |
|
---|
42 | <!-- eslint-disable no-undef -->
|
---|
43 |
|
---|
44 | ```js
|
---|
45 | status(403) // => 'Forbidden'
|
---|
46 | status('403') // => 'Forbidden'
|
---|
47 | status(306) // throws
|
---|
48 | ```
|
---|
49 |
|
---|
50 | ### status(msg)
|
---|
51 |
|
---|
52 | Returns the numeric status code for a known HTTP status message. The message
|
---|
53 | is case-insensitive. An error is thrown for an unknown status message.
|
---|
54 |
|
---|
55 | <!-- eslint-disable no-undef -->
|
---|
56 |
|
---|
57 | ```js
|
---|
58 | status('forbidden') // => 403
|
---|
59 | status('Forbidden') // => 403
|
---|
60 | status('foo') // throws
|
---|
61 | ```
|
---|
62 |
|
---|
63 | ### status.codes
|
---|
64 |
|
---|
65 | Returns an array of all the status codes as `Integer`s.
|
---|
66 |
|
---|
67 | ### status.code[msg]
|
---|
68 |
|
---|
69 | Returns the numeric status code for a known status message (in lower-case),
|
---|
70 | otherwise `undefined`.
|
---|
71 |
|
---|
72 | <!-- eslint-disable no-undef, no-unused-expressions -->
|
---|
73 |
|
---|
74 | ```js
|
---|
75 | status['not found'] // => 404
|
---|
76 | ```
|
---|
77 |
|
---|
78 | ### status.empty[code]
|
---|
79 |
|
---|
80 | Returns `true` if a status code expects an empty body.
|
---|
81 |
|
---|
82 | <!-- eslint-disable no-undef, no-unused-expressions -->
|
---|
83 |
|
---|
84 | ```js
|
---|
85 | status.empty[200] // => undefined
|
---|
86 | status.empty[204] // => true
|
---|
87 | status.empty[304] // => true
|
---|
88 | ```
|
---|
89 |
|
---|
90 | ### status.message[code]
|
---|
91 |
|
---|
92 | Returns 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
|
---|
99 | status.message[404] // => 'Not Found'
|
---|
100 | ```
|
---|
101 |
|
---|
102 | ### status.redirect[code]
|
---|
103 |
|
---|
104 | Returns `true` if a status code is a valid redirect status.
|
---|
105 |
|
---|
106 | <!-- eslint-disable no-undef, no-unused-expressions -->
|
---|
107 |
|
---|
108 | ```js
|
---|
109 | status.redirect[200] // => undefined
|
---|
110 | status.redirect[301] // => true
|
---|
111 | ```
|
---|
112 |
|
---|
113 | ### status.retry[code]
|
---|
114 |
|
---|
115 | Returns `true` if you should retry the rest.
|
---|
116 |
|
---|
117 | <!-- eslint-disable no-undef, no-unused-expressions -->
|
---|
118 |
|
---|
119 | ```js
|
---|
120 | status.retry[501] // => undefined
|
---|
121 | status.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
|
---|