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