source: node_modules/accepts/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
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][github-actions-ci-image]][github-actions-ci-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```js
33var accepts = require('accepts')
34```
35
36### accepts(req)
37
38Create a new `Accepts` object for the given `req`.
39
40#### .charset(charsets)
41
42Return the first accepted charset. If nothing in `charsets` is accepted,
43then `false` is returned.
44
45#### .charsets()
46
47Return the charsets that the request accepts, in the order of the client's
48preference (most preferred first).
49
50#### .encoding(encodings)
51
52Return the first accepted encoding. If nothing in `encodings` is accepted,
53then `false` is returned.
54
55#### .encodings()
56
57Return the encodings that the request accepts, in the order of the client's
58preference (most preferred first).
59
60#### .language(languages)
61
62Return the first accepted language. If nothing in `languages` is accepted,
63then `false` is returned.
64
65#### .languages()
66
67Return the languages that the request accepts, in the order of the client's
68preference (most preferred first).
69
70#### .type(types)
71
72Return the first accepted type (and it is returned as the same text as what
73appears in the `types` array). If nothing in `types` is accepted, then `false`
74is returned.
75
76The `types` array can contain full MIME types or file extensions. Any value
77that is not a full MIME types is passed to `require('mime-types').lookup`.
78
79#### .types()
80
81Return the types that the request accepts, in the order of the client's
82preference (most preferred first).
83
84## Examples
85
86### Simple type negotiation
87
88This simple example shows how to use `accepts` to return a different typed
89respond body based on what the client wants to accept. The server lists it's
90preferences in order and will get back the best match between the client and
91server.
92
93```js
94var accepts = require('accepts')
95var http = require('http')
96
97function app (req, res) {
98 var accept = accepts(req)
99
100 // the order of this list is significant; should be server preferred order
101 switch (accept.type(['json', 'html'])) {
102 case 'json':
103 res.setHeader('Content-Type', 'application/json')
104 res.write('{"hello":"world!"}')
105 break
106 case 'html':
107 res.setHeader('Content-Type', 'text/html')
108 res.write('<b>hello, world!</b>')
109 break
110 default:
111 // the fallback is text/plain, so no need to specify it above
112 res.setHeader('Content-Type', 'text/plain')
113 res.write('hello, world!')
114 break
115 }
116
117 res.end()
118}
119
120http.createServer(app).listen(3000)
121```
122
123You can test this out with the cURL program:
124```sh
125curl -I -H'Accept: text/html' http://localhost:3000/
126```
127
128## License
129
130[MIT](LICENSE)
131
132[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master
133[coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master
134[github-actions-ci-image]: https://badgen.net/github/checks/jshttp/accepts/master?label=ci
135[github-actions-ci-url]: https://github.com/jshttp/accepts/actions/workflows/ci.yml
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
Note: See TracBrowser for help on using the repository browser.