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.

94 lines
2.8 KiB

7 years ago
  1. # accepts
  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. Higher level content negotation based on [negotiator](https://github.com/federomero/negotiator). Extracted from [koa](https://github.com/koajs/koa) for general use.
  8. In addition to negotatior, it allows:
  9. - Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` as well as `('text/html', 'application/json')`.
  10. - Allows type shorthands such as `json`.
  11. - Returns `false` when no types match
  12. - Treats non-existent headers as `*`
  13. ## API
  14. ### var accept = new Accepts(req)
  15. ```js
  16. var accepts = require('accepts')
  17. http.createServer(function (req, res) {
  18. var accept = accepts(req)
  19. })
  20. ```
  21. ### accept\[property\]\(\)
  22. Returns all the explicitly accepted content property as an array in descending priority.
  23. - `accept.types()`
  24. - `accept.encodings()`
  25. - `accept.charsets()`
  26. - `accept.languages()`
  27. They are also aliased in singular form such as `accept.type()`. `accept.languages()` is also aliased as `accept.langs()`, etc.
  28. Note: you should almost never do this in a real app as it defeats the purpose of content negotiation.
  29. Example:
  30. ```js
  31. // in Google Chrome
  32. var encodings = accept.encodings() // -> ['sdch', 'gzip', 'deflate']
  33. ```
  34. Since you probably don't support `sdch`, you should just supply the encodings you support:
  35. ```js
  36. var encoding = accept.encodings('gzip', 'deflate') // -> 'gzip', probably
  37. ```
  38. ### accept\[property\]\(values, ...\)
  39. You can either have `values` be an array or have an argument list of values.
  40. If the client does not accept any `values`, `false` will be returned.
  41. If the client accepts any `values`, the preferred `value` will be return.
  42. For `accept.types()`, shorthand mime types are allowed.
  43. Example:
  44. ```js
  45. // req.headers.accept = 'application/json'
  46. accept.types('json') // -> 'json'
  47. accept.types('html', 'json') // -> 'json'
  48. accept.types('html') // -> false
  49. // req.headers.accept = ''
  50. // which is equivalent to `*`
  51. accept.types() // -> [], no explicit types
  52. accept.types('text/html', 'text/json') // -> 'text/html', since it was first
  53. ```
  54. ## License
  55. [MIT](LICENSE)
  56. [npm-image]: https://img.shields.io/npm/v/accepts.svg?style=flat
  57. [npm-url]: https://npmjs.org/package/accepts
  58. [node-version-image]: https://img.shields.io/node/v/accepts.svg?style=flat
  59. [node-version-url]: http://nodejs.org/download/
  60. [travis-image]: https://img.shields.io/travis/jshttp/accepts.svg?style=flat
  61. [travis-url]: https://travis-ci.org/jshttp/accepts
  62. [coveralls-image]: https://img.shields.io/coveralls/jshttp/accepts.svg?style=flat
  63. [coveralls-url]: https://coveralls.io/r/jshttp/accepts
  64. [downloads-image]: https://img.shields.io/npm/dm/accepts.svg?style=flat
  65. [downloads-url]: https://npmjs.org/package/accepts