UNPKG

4.81 kBMarkdownView Raw
1# negotiator
2
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]
8
9An HTTP content negotiator for Node.js
10
11## Installation
12
13```sh
14$ npm install negotiator
15```
16
17## API
18
19```js
20var Negotiator = require('negotiator')
21```
22
23### Accept Negotiation
24
25```js
26availableMediaTypes = ['text/html', 'text/plain', 'application/json']
27
28// The negotiator constructor receives a request object
29negotiator = new Negotiator(request)
30
31// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
32
33negotiator.mediaTypes()
34// -> ['text/html', 'image/jpeg', 'application/*']
35
36negotiator.mediaTypes(availableMediaTypes)
37// -> ['text/html', 'application/json']
38
39negotiator.mediaType(availableMediaTypes)
40// -> 'text/html'
41```
42
43You can check a working example at `examples/accept.js`.
44
45#### Methods
46
47##### mediaType()
48
49Returns the most preferred media type from the client.
50
51##### mediaType(availableMediaType)
52
53Returns the most preferred media type from a list of available media types.
54
55##### mediaTypes()
56
57Returns an array of preferred media types ordered by the client preference.
58
59##### mediaTypes(availableMediaTypes)
60
61Returns an array of preferred media types ordered by priority from a list of
62available media types.
63
64### Accept-Language Negotiation
65
66```js
67negotiator = new Negotiator(request)
68
69availableLanguages = ['en', 'es', 'fr']
70
71// Let's say Accept-Language header is 'en;q=0.8, es, pt'
72
73negotiator.languages()
74// -> ['es', 'pt', 'en']
75
76negotiator.languages(availableLanguages)
77// -> ['es', 'en']
78
79language = negotiator.language(availableLanguages)
80// -> 'es'
81```
82
83You can check a working example at `examples/language.js`.
84
85#### Methods
86
87##### language()
88
89Returns the most preferred language from the client.
90
91##### language(availableLanguages)
92
93Returns the most preferred language from a list of available languages.
94
95##### languages()
96
97Returns an array of preferred languages ordered by the client preference.
98
99##### languages(availableLanguages)
100
101Returns an array of preferred languages ordered by priority from a list of
102available languages.
103
104### Accept-Charset Negotiation
105
106```js
107availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
108
109negotiator = new Negotiator(request)
110
111// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
112
113negotiator.charsets()
114// -> ['utf-8', 'iso-8859-1', 'utf-7']
115
116negotiator.charsets(availableCharsets)
117// -> ['utf-8', 'iso-8859-1']
118
119negotiator.charset(availableCharsets)
120// -> 'utf-8'
121```
122
123You can check a working example at `examples/charset.js`.
124
125#### Methods
126
127##### charset()
128
129Returns the most preferred charset from the client.
130
131##### charset(availableCharsets)
132
133Returns the most preferred charset from a list of available charsets.
134
135##### charsets()
136
137Returns an array of preferred charsets ordered by the client preference.
138
139##### charsets(availableCharsets)
140
141Returns an array of preferred charsets ordered by priority from a list of
142available charsets.
143
144### Accept-Encoding Negotiation
145
146```js
147availableEncodings = ['identity', 'gzip']
148
149negotiator = new Negotiator(request)
150
151// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
152
153negotiator.encodings()
154// -> ['gzip', 'identity', 'compress']
155
156negotiator.encodings(availableEncodings)
157// -> ['gzip', 'identity']
158
159negotiator.encoding(availableEncodings)
160// -> 'gzip'
161```
162
163You can check a working example at `examples/encoding.js`.
164
165#### Methods
166
167##### encoding()
168
169Returns the most preferred encoding from the client.
170
171##### encoding(availableEncodings)
172
173Returns the most preferred encoding from a list of available encodings.
174
175##### encodings()
176
177Returns an array of preferred encodings ordered by the client preference.
178
179##### encodings(availableEncodings)
180
181Returns an array of preferred encodings ordered by priority from a list of
182available encodings.
183
184## See Also
185
186The [accepts](https://npmjs.org/package/accepts#readme) module builds on
187this module and provides an alternative interface, mime type validation,
188and more.
189
190## License
191
192[MIT](LICENSE)
193
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