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.

161 lines
3.9 KiB

7 years ago
  1. # negotiator
  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. An HTTP content negotiator for Node.js
  8. ## Installation
  9. ```sh
  10. $ npm install negotiator
  11. ```
  12. ## API
  13. ```js
  14. var Negotiator = require('negotiator')
  15. ```
  16. ### Accept Negotiation
  17. ```js
  18. availableMediaTypes = ['text/html', 'text/plain', 'application/json']
  19. // The negotiator constructor receives a request object
  20. negotiator = new Negotiator(request)
  21. // Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
  22. negotiator.mediaTypes()
  23. // -> ['text/html', 'image/jpeg', 'application/*']
  24. negotiator.mediaTypes(availableMediaTypes)
  25. // -> ['text/html', 'application/json']
  26. negotiator.mediaType(availableMediaTypes)
  27. // -> 'text/html'
  28. ```
  29. You can check a working example at `examples/accept.js`.
  30. #### Methods
  31. ##### mediaTypes(availableMediaTypes):
  32. Returns an array of preferred media types ordered by priority from a list of available media types.
  33. ##### mediaType(availableMediaType):
  34. Returns the top preferred media type from a list of available media types.
  35. ### Accept-Language Negotiation
  36. ```js
  37. negotiator = new Negotiator(request)
  38. availableLanguages = 'en', 'es', 'fr'
  39. // Let's say Accept-Language header is 'en;q=0.8, es, pt'
  40. negotiator.languages()
  41. // -> ['es', 'pt', 'en']
  42. negotiator.languages(availableLanguages)
  43. // -> ['es', 'en']
  44. language = negotiator.language(availableLanguages)
  45. // -> 'es'
  46. ```
  47. You can check a working example at `examples/language.js`.
  48. #### Methods
  49. ##### languages(availableLanguages):
  50. Returns an array of preferred languages ordered by priority from a list of available languages.
  51. ##### language(availableLanguages):
  52. Returns the top preferred language from a list of available languages.
  53. ### Accept-Charset Negotiation
  54. ```js
  55. availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
  56. negotiator = new Negotiator(request)
  57. // Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
  58. negotiator.charsets()
  59. // -> ['utf-8', 'iso-8859-1', 'utf-7']
  60. negotiator.charsets(availableCharsets)
  61. // -> ['utf-8', 'iso-8859-1']
  62. negotiator.charset(availableCharsets)
  63. // -> 'utf-8'
  64. ```
  65. You can check a working example at `examples/charset.js`.
  66. #### Methods
  67. ##### charsets(availableCharsets):
  68. Returns an array of preferred charsets ordered by priority from a list of available charsets.
  69. ##### charset(availableCharsets):
  70. Returns the top preferred charset from a list of available charsets.
  71. ### Accept-Encoding Negotiation
  72. ```js
  73. availableEncodings = ['identity', 'gzip']
  74. negotiator = new Negotiator(request)
  75. // Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
  76. negotiator.encodings()
  77. // -> ['gzip', 'identity', 'compress']
  78. negotiator.encodings(availableEncodings)
  79. // -> ['gzip', 'identity']
  80. negotiator.encoding(availableEncodings)
  81. // -> 'gzip'
  82. ```
  83. You can check a working example at `examples/encoding.js`.
  84. #### Methods
  85. ##### encodings(availableEncodings):
  86. Returns an array of preferred encodings ordered by priority from a list of available encodings.
  87. ##### encoding(availableEncodings):
  88. Returns the top preferred encoding from a list of available encodings.
  89. ## License
  90. [MIT](LICENSE)
  91. [npm-image]: https://img.shields.io/npm/v/negotiator.svg?style=flat
  92. [npm-url]: https://npmjs.org/package/negotiator
  93. [node-version-image]: https://img.shields.io/node/v/negotiator.svg?style=flat
  94. [node-version-url]: http://nodejs.org/download/
  95. [travis-image]: https://img.shields.io/travis/jshttp/negotiator.svg?style=flat
  96. [travis-url]: https://travis-ci.org/jshttp/negotiator
  97. [coveralls-image]: https://img.shields.io/coveralls/jshttp/negotiator.svg?style=flat
  98. [coveralls-url]: https://coveralls.io/r/jshttp/negotiator?branch=master
  99. [downloads-image]: https://img.shields.io/npm/dm/negotiator.svg?style=flat
  100. [downloads-url]: https://npmjs.org/package/negotiator