You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.5 KiB

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