1 |
3a515b92
|
cagy
|
# accepts
|
2 |
|
|
|
3 |
|
|
[![NPM Version][npm-version-image]][npm-url]
|
4 |
|
|
[![NPM Downloads][npm-downloads-image]][npm-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 |
|
|
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).
|
10 |
|
|
Extracted from [koa](https://www.npmjs.com/package/koa) for general use.
|
11 |
|
|
|
12 |
|
|
In addition to negotiator, it allows:
|
13 |
|
|
|
14 |
|
|
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`
|
15 |
|
|
as well as `('text/html', 'application/json')`.
|
16 |
|
|
- Allows type shorthands such as `json`.
|
17 |
|
|
- Returns `false` when no types match
|
18 |
|
|
- Treats non-existent headers as `*`
|
19 |
|
|
|
20 |
|
|
## Installation
|
21 |
|
|
|
22 |
|
|
This is a [Node.js](https://nodejs.org/en/) module available through the
|
23 |
|
|
[npm registry](https://www.npmjs.com/). Installation is done using the
|
24 |
|
|
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
25 |
|
|
|
26 |
|
|
```sh
|
27 |
|
|
$ npm install accepts
|
28 |
|
|
```
|
29 |
|
|
|
30 |
|
|
## API
|
31 |
|
|
|
32 |
|
|
<!-- eslint-disable no-unused-vars -->
|
33 |
|
|
|
34 |
|
|
```js
|
35 |
|
|
var accepts = require('accepts')
|
36 |
|
|
```
|
37 |
|
|
|
38 |
|
|
### accepts(req)
|
39 |
|
|
|
40 |
|
|
Create a new `Accepts` object for the given `req`.
|
41 |
|
|
|
42 |
|
|
#### .charset(charsets)
|
43 |
|
|
|
44 |
|
|
Return the first accepted charset. If nothing in `charsets` is accepted,
|
45 |
|
|
then `false` is returned.
|
46 |
|
|
|
47 |
|
|
#### .charsets()
|
48 |
|
|
|
49 |
|
|
Return the charsets that the request accepts, in the order of the client's
|
50 |
|
|
preference (most preferred first).
|
51 |
|
|
|
52 |
|
|
#### .encoding(encodings)
|
53 |
|
|
|
54 |
|
|
Return the first accepted encoding. If nothing in `encodings` is accepted,
|
55 |
|
|
then `false` is returned.
|
56 |
|
|
|
57 |
|
|
#### .encodings()
|
58 |
|
|
|
59 |
|
|
Return the encodings that the request accepts, in the order of the client's
|
60 |
|
|
preference (most preferred first).
|
61 |
|
|
|
62 |
|
|
#### .language(languages)
|
63 |
|
|
|
64 |
|
|
Return the first accepted language. If nothing in `languages` is accepted,
|
65 |
|
|
then `false` is returned.
|
66 |
|
|
|
67 |
|
|
#### .languages()
|
68 |
|
|
|
69 |
|
|
Return the languages that the request accepts, in the order of the client's
|
70 |
|
|
preference (most preferred first).
|
71 |
|
|
|
72 |
|
|
#### .type(types)
|
73 |
|
|
|
74 |
|
|
Return the first accepted type (and it is returned as the same text as what
|
75 |
|
|
appears in the `types` array). If nothing in `types` is accepted, then `false`
|
76 |
|
|
is returned.
|
77 |
|
|
|
78 |
|
|
The `types` array can contain full MIME types or file extensions. Any value
|
79 |
|
|
that is not a full MIME types is passed to `require('mime-types').lookup`.
|
80 |
|
|
|
81 |
|
|
#### .types()
|
82 |
|
|
|
83 |
|
|
Return the types that the request accepts, in the order of the client's
|
84 |
|
|
preference (most preferred first).
|
85 |
|
|
|
86 |
|
|
## Examples
|
87 |
|
|
|
88 |
|
|
### Simple type negotiation
|
89 |
|
|
|
90 |
|
|
This simple example shows how to use `accepts` to return a different typed
|
91 |
|
|
respond body based on what the client wants to accept. The server lists it's
|
92 |
|
|
preferences in order and will get back the best match between the client and
|
93 |
|
|
server.
|
94 |
|
|
|
95 |
|
|
```js
|
96 |
|
|
var accepts = require('accepts')
|
97 |
|
|
var http = require('http')
|
98 |
|
|
|
99 |
|
|
function app (req, res) {
|
100 |
|
|
var accept = accepts(req)
|
101 |
|
|
|
102 |
|
|
// the order of this list is significant; should be server preferred order
|
103 |
|
|
switch (accept.type(['json', 'html'])) {
|
104 |
|
|
case 'json':
|
105 |
|
|
res.setHeader('Content-Type', 'application/json')
|
106 |
|
|
res.write('{"hello":"world!"}')
|
107 |
|
|
break
|
108 |
|
|
case 'html':
|
109 |
|
|
res.setHeader('Content-Type', 'text/html')
|
110 |
|
|
res.write('<b>hello, world!</b>')
|
111 |
|
|
break
|
112 |
|
|
default:
|
113 |
|
|
// the fallback is text/plain, so no need to specify it above
|
114 |
|
|
res.setHeader('Content-Type', 'text/plain')
|
115 |
|
|
res.write('hello, world!')
|
116 |
|
|
break
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
res.end()
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
http.createServer(app).listen(3000)
|
123 |
|
|
```
|
124 |
|
|
|
125 |
|
|
You can test this out with the cURL program:
|
126 |
|
|
```sh
|
127 |
|
|
curl -I -H'Accept: text/html' http://localhost:3000/
|
128 |
|
|
```
|
129 |
|
|
|
130 |
|
|
## License
|
131 |
|
|
|
132 |
|
|
[MIT](LICENSE)
|
133 |
|
|
|
134 |
|
|
[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master
|
135 |
|
|
[coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master
|
136 |
|
|
[node-version-image]: https://badgen.net/npm/node/accepts
|
137 |
|
|
[node-version-url]: https://nodejs.org/en/download
|
138 |
|
|
[npm-downloads-image]: https://badgen.net/npm/dm/accepts
|
139 |
|
|
[npm-url]: https://npmjs.org/package/accepts
|
140 |
|
|
[npm-version-image]: https://badgen.net/npm/v/accepts
|
141 |
|
|
[travis-image]: https://badgen.net/travis/jshttp/accepts/master
|
142 |
|
|
[travis-url]: https://travis-ci.org/jshttp/accepts
|