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.

140 lines
4.0 KiB

  1. # accepts
  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][github-actions-ci-image]][github-actions-ci-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).
  8. Extracted from [koa](https://www.npmjs.com/package/koa) for general use.
  9. In addition to negotiator, it allows:
  10. - Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`
  11. as well as `('text/html', 'application/json')`.
  12. - Allows type shorthands such as `json`.
  13. - Returns `false` when no types match
  14. - Treats non-existent headers as `*`
  15. ## Installation
  16. This is a [Node.js](https://nodejs.org/en/) module available through the
  17. [npm registry](https://www.npmjs.com/). Installation is done using the
  18. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  19. ```sh
  20. $ npm install accepts
  21. ```
  22. ## API
  23. ```js
  24. var accepts = require('accepts')
  25. ```
  26. ### accepts(req)
  27. Create a new `Accepts` object for the given `req`.
  28. #### .charset(charsets)
  29. Return the first accepted charset. If nothing in `charsets` is accepted,
  30. then `false` is returned.
  31. #### .charsets()
  32. Return the charsets that the request accepts, in the order of the client's
  33. preference (most preferred first).
  34. #### .encoding(encodings)
  35. Return the first accepted encoding. If nothing in `encodings` is accepted,
  36. then `false` is returned.
  37. #### .encodings()
  38. Return the encodings that the request accepts, in the order of the client's
  39. preference (most preferred first).
  40. #### .language(languages)
  41. Return the first accepted language. If nothing in `languages` is accepted,
  42. then `false` is returned.
  43. #### .languages()
  44. Return the languages that the request accepts, in the order of the client's
  45. preference (most preferred first).
  46. #### .type(types)
  47. Return the first accepted type (and it is returned as the same text as what
  48. appears in the `types` array). If nothing in `types` is accepted, then `false`
  49. is returned.
  50. The `types` array can contain full MIME types or file extensions. Any value
  51. that is not a full MIME types is passed to `require('mime-types').lookup`.
  52. #### .types()
  53. Return the types that the request accepts, in the order of the client's
  54. preference (most preferred first).
  55. ## Examples
  56. ### Simple type negotiation
  57. This simple example shows how to use `accepts` to return a different typed
  58. respond body based on what the client wants to accept. The server lists it's
  59. preferences in order and will get back the best match between the client and
  60. server.
  61. ```js
  62. var accepts = require('accepts')
  63. var http = require('http')
  64. function app (req, res) {
  65. var accept = accepts(req)
  66. // the order of this list is significant; should be server preferred order
  67. switch (accept.type(['json', 'html'])) {
  68. case 'json':
  69. res.setHeader('Content-Type', 'application/json')
  70. res.write('{"hello":"world!"}')
  71. break
  72. case 'html':
  73. res.setHeader('Content-Type', 'text/html')
  74. res.write('<b>hello, world!</b>')
  75. break
  76. default:
  77. // the fallback is text/plain, so no need to specify it above
  78. res.setHeader('Content-Type', 'text/plain')
  79. res.write('hello, world!')
  80. break
  81. }
  82. res.end()
  83. }
  84. http.createServer(app).listen(3000)
  85. ```
  86. You can test this out with the cURL program:
  87. ```sh
  88. curl -I -H'Accept: text/html' http://localhost:3000/
  89. ```
  90. ## License
  91. [MIT](LICENSE)
  92. [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master
  93. [coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master
  94. [github-actions-ci-image]: https://badgen.net/github/checks/jshttp/accepts/master?label=ci
  95. [github-actions-ci-url]: https://github.com/jshttp/accepts/actions/workflows/ci.yml
  96. [node-version-image]: https://badgen.net/npm/node/accepts
  97. [node-version-url]: https://nodejs.org/en/download
  98. [npm-downloads-image]: https://badgen.net/npm/dm/accepts
  99. [npm-url]: https://npmjs.org/package/accepts
  100. [npm-version-image]: https://badgen.net/npm/v/accepts