00d65ccf7423b645489d6f210c90b1e0a9b74e5cc75bf86c817f83a4571dd5da37592ed0b2d9781b29322ff9227f7df43af80a17fabb1c5ed251c6cf53d998 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Statuses
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. HTTP status utility for node.
  8. ## API
  9. ```js
  10. var status = require('statuses')
  11. ```
  12. ### var code = status(Integer || String)
  13. If `Integer` or `String` is a valid HTTP code or status message, then the appropriate `code` will be returned. Otherwise, an error will be thrown.
  14. ```js
  15. status(403) // => 403
  16. status('403') // => 403
  17. status('forbidden') // => 403
  18. status('Forbidden') // => 403
  19. status(306) // throws, as it's not supported by node.js
  20. ```
  21. ### status.codes
  22. Returns an array of all the status codes as `Integer`s.
  23. ### var msg = status[code]
  24. Map of `code` to `status message`. `undefined` for invalid `code`s.
  25. ```js
  26. status[404] // => 'Not Found'
  27. ```
  28. ### var code = status[msg]
  29. Map of `status message` to `code`. `msg` can either be title-cased or lower-cased. `undefined` for invalid `status message`s.
  30. ```js
  31. status['not found'] // => 404
  32. status['Not Found'] // => 404
  33. ```
  34. ### status.redirect[code]
  35. Returns `true` if a status code is a valid redirect status.
  36. ```js
  37. status.redirect[200] // => undefined
  38. status.redirect[301] // => true
  39. ```
  40. ### status.empty[code]
  41. Returns `true` if a status code expects an empty body.
  42. ```js
  43. status.empty[200] // => undefined
  44. status.empty[204] // => true
  45. status.empty[304] // => true
  46. ```
  47. ### status.retry[code]
  48. Returns `true` if you should retry the rest.
  49. ```js
  50. status.retry[501] // => undefined
  51. status.retry[503] // => true
  52. ```
  53. ## Adding Status Codes
  54. The status codes are primarily sourced from http://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv.
  55. Additionally, custom codes are added from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes.
  56. These are added manually in the `lib/*.json` files.
  57. If you would like to add a status code, add it to the appropriate JSON file.
  58. To rebuild `codes.json`, run the following:
  59. ```bash
  60. # update src/iana.json
  61. npm run fetch
  62. # build codes.json
  63. npm run build
  64. ```
  65. [npm-image]: https://img.shields.io/npm/v/statuses.svg
  66. [npm-url]: https://npmjs.org/package/statuses
  67. [node-version-image]: https://img.shields.io/badge/node.js-%3E%3D_0.6-brightgreen.svg
  68. [node-version-url]: https://nodejs.org/en/download
  69. [travis-image]: https://img.shields.io/travis/jshttp/statuses.svg
  70. [travis-url]: https://travis-ci.org/jshttp/statuses
  71. [coveralls-image]: https://img.shields.io/coveralls/jshttp/statuses.svg
  72. [coveralls-url]: https://coveralls.io/r/jshttp/statuses?branch=master
  73. [downloads-image]: https://img.shields.io/npm/dm/statuses.svg
  74. [downloads-url]: https://npmjs.org/package/statuses