3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 An HTTP content negotiator for Node.js
14 $ npm install negotiator
20 var Negotiator = require('negotiator')
23 ### Accept Negotiation
26 availableMediaTypes = ['text/html', 'text/plain', 'application/json']
28 // The negotiator constructor receives a request object
29 negotiator = new Negotiator(request)
31 // Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
33 negotiator.mediaTypes()
34 // -> ['text/html', 'image/jpeg', 'application/*']
36 negotiator.mediaTypes(availableMediaTypes)
37 // -> ['text/html', 'application/json']
39 negotiator.mediaType(availableMediaTypes)
43 You can check a working example at `examples/accept.js`.
49 Returns the most preferred media type from the client.
51 ##### mediaType(availableMediaType)
53 Returns the most preferred media type from a list of available media types.
57 Returns an array of preferred media types ordered by the client preference.
59 ##### mediaTypes(availableMediaTypes)
61 Returns an array of preferred media types ordered by priority from a list of
62 available media types.
64 ### Accept-Language Negotiation
67 negotiator = new Negotiator(request)
69 availableLanguages = ['en', 'es', 'fr']
71 // Let's say Accept-Language header is 'en;q=0.8, es, pt'
73 negotiator.languages()
74 // -> ['es', 'pt', 'en']
76 negotiator.languages(availableLanguages)
79 language = negotiator.language(availableLanguages)
83 You can check a working example at `examples/language.js`.
89 Returns the most preferred language from the client.
91 ##### language(availableLanguages)
93 Returns the most preferred language from a list of available languages.
97 Returns an array of preferred languages ordered by the client preference.
99 ##### languages(availableLanguages)
101 Returns an array of preferred languages ordered by priority from a list of
104 ### Accept-Charset Negotiation
107 availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
109 negotiator = new Negotiator(request)
111 // Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
113 negotiator.charsets()
114 // -> ['utf-8', 'iso-8859-1', 'utf-7']
116 negotiator.charsets(availableCharsets)
117 // -> ['utf-8', 'iso-8859-1']
119 negotiator.charset(availableCharsets)
123 You can check a working example at `examples/charset.js`.
129 Returns the most preferred charset from the client.
131 ##### charset(availableCharsets)
133 Returns the most preferred charset from a list of available charsets.
137 Returns an array of preferred charsets ordered by the client preference.
139 ##### charsets(availableCharsets)
141 Returns an array of preferred charsets ordered by priority from a list of
144 ### Accept-Encoding Negotiation
147 availableEncodings = ['identity', 'gzip']
149 negotiator = new Negotiator(request)
151 // Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
153 negotiator.encodings()
154 // -> ['gzip', 'identity', 'compress']
156 negotiator.encodings(availableEncodings)
157 // -> ['gzip', 'identity']
159 negotiator.encoding(availableEncodings)
163 You can check a working example at `examples/encoding.js`.
169 Returns the most preferred encoding from the client.
171 ##### encoding(availableEncodings)
173 Returns the most preferred encoding from a list of available encodings.
177 Returns an array of preferred encodings ordered by the client preference.
179 ##### encodings(availableEncodings)
181 Returns an array of preferred encodings ordered by priority from a list of
186 The [accepts](https://npmjs.org/package/accepts#readme) module builds on
187 this module and provides an alternative interface, mime type validation,
194 [npm-image]: https://img.shields.io/npm/v/negotiator.svg
195 [npm-url]: https://npmjs.org/package/negotiator
196 [node-version-image]: https://img.shields.io/node/v/negotiator.svg
197 [node-version-url]: https://nodejs.org/en/download/
198 [travis-image]: https://img.shields.io/travis/jshttp/negotiator/master.svg
199 [travis-url]: https://travis-ci.org/jshttp/negotiator
200 [coveralls-image]: https://img.shields.io/coveralls/jshttp/negotiator/master.svg
201 [coveralls-url]: https://coveralls.io/r/jshttp/negotiator?branch=master
202 [downloads-image]: https://img.shields.io/npm/dm/negotiator.svg
203 [downloads-url]: https://npmjs.org/package/negotiator