選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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][travis-image]][travis-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. <!-- eslint-disable no-unused-vars -->
  24. ```js
  25. var accepts = require('accepts')
  26. ```
  27. ### accepts(req)
  28. Create a new `Accepts` object for the given `req`.
  29. #### .charset(charsets)
  30. Return the first accepted charset. If nothing in `charsets` is accepted,
  31. then `false` is returned.
  32. #### .charsets()
  33. Return the charsets that the request accepts, in the order of the client's
  34. preference (most preferred first).
  35. #### .encoding(encodings)
  36. Return the first accepted encoding. If nothing in `encodings` is accepted,
  37. then `false` is returned.
  38. #### .encodings()
  39. Return the encodings that the request accepts, in the order of the client's
  40. preference (most preferred first).
  41. #### .language(languages)
  42. Return the first accepted language. If nothing in `languages` is accepted,
  43. then `false` is returned.
  44. #### .languages()
  45. Return the languages that the request accepts, in the order of the client's
  46. preference (most preferred first).
  47. #### .type(types)
  48. Return the first accepted type (and it is returned as the same text as what
  49. appears in the `types` array). If nothing in `types` is accepted, then `false`
  50. is returned.
  51. The `types` array can contain full MIME types or file extensions. Any value
  52. that is not a full MIME types is passed to `require('mime-types').lookup`.
  53. #### .types()
  54. Return the types that the request accepts, in the order of the client's
  55. preference (most preferred first).
  56. ## Examples
  57. ### Simple type negotiation
  58. This simple example shows how to use `accepts` to return a different typed
  59. respond body based on what the client wants to accept. The server lists it's
  60. preferences in order and will get back the best match between the client and
  61. server.
  62. ```js
  63. var accepts = require('accepts')
  64. var http = require('http')
  65. function app (req, res) {
  66. var accept = accepts(req)
  67. // the order of this list is significant; should be server preferred order
  68. switch (accept.type(['json', 'html'])) {
  69. case 'json':
  70. res.setHeader('Content-Type', 'application/json')
  71. res.write('{"hello":"world!"}')
  72. break
  73. case 'html':
  74. res.setHeader('Content-Type', 'text/html')
  75. res.write('<b>hello, world!</b>')
  76. break
  77. default:
  78. // the fallback is text/plain, so no need to specify it above
  79. res.setHeader('Content-Type', 'text/plain')
  80. res.write('hello, world!')
  81. break
  82. }
  83. res.end()
  84. }
  85. http.createServer(app).listen(3000)
  86. ```
  87. You can test this out with the cURL program:
  88. ```sh
  89. curl -I -H'Accept: text/html' http://localhost:3000/
  90. ```
  91. ## License
  92. [MIT](LICENSE)
  93. [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master
  94. [coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master
  95. [node-version-image]: https://badgen.net/npm/node/accepts
  96. [node-version-url]: https://nodejs.org/en/download
  97. [npm-downloads-image]: https://badgen.net/npm/dm/accepts
  98. [npm-url]: https://npmjs.org/package/accepts
  99. [npm-version-image]: https://badgen.net/npm/v/accepts
  100. [travis-image]: https://badgen.net/travis/jshttp/accepts/master
  101. [travis-url]: https://travis-ci.org/jshttp/accepts