source: trip-planner-front/node_modules/accepts/README.md@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[6a3a178]1# 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
9Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).
10Extracted from [koa](https://www.npmjs.com/package/koa) for general use.
11
12In 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
22This 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
35var accepts = require('accepts')
36```
37
38### accepts(req)
39
40Create a new `Accepts` object for the given `req`.
41
42#### .charset(charsets)
43
44Return the first accepted charset. If nothing in `charsets` is accepted,
45then `false` is returned.
46
47#### .charsets()
48
49Return the charsets that the request accepts, in the order of the client's
50preference (most preferred first).
51
52#### .encoding(encodings)
53
54Return the first accepted encoding. If nothing in `encodings` is accepted,
55then `false` is returned.
56
57#### .encodings()
58
59Return the encodings that the request accepts, in the order of the client's
60preference (most preferred first).
61
62#### .language(languages)
63
64Return the first accepted language. If nothing in `languages` is accepted,
65then `false` is returned.
66
67#### .languages()
68
69Return the languages that the request accepts, in the order of the client's
70preference (most preferred first).
71
72#### .type(types)
73
74Return the first accepted type (and it is returned as the same text as what
75appears in the `types` array). If nothing in `types` is accepted, then `false`
76is returned.
77
78The `types` array can contain full MIME types or file extensions. Any value
79that is not a full MIME types is passed to `require('mime-types').lookup`.
80
81#### .types()
82
83Return the types that the request accepts, in the order of the client's
84preference (most preferred first).
85
86## Examples
87
88### Simple type negotiation
89
90This simple example shows how to use `accepts` to return a different typed
91respond body based on what the client wants to accept. The server lists it's
92preferences in order and will get back the best match between the client and
93server.
94
95```js
96var accepts = require('accepts')
97var http = require('http')
98
99function 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
122http.createServer(app).listen(3000)
123```
124
125You can test this out with the cURL program:
126```sh
127curl -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
Note: See TracBrowser for help on using the repository browser.