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]
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.
12 In addition to negotiator, it allows:
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 `*`
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):
32 <!-- eslint-disable no-unused-vars -->
35 var accepts = require('accepts')
40 Create a new `Accepts` object for the given `req`.
42 #### .charset(charsets)
44 Return the first accepted charset. If nothing in `charsets` is accepted,
45 then `false` is returned.
49 Return the charsets that the request accepts, in the order of the client's
50 preference (most preferred first).
52 #### .encoding(encodings)
54 Return the first accepted encoding. If nothing in `encodings` is accepted,
55 then `false` is returned.
59 Return the encodings that the request accepts, in the order of the client's
60 preference (most preferred first).
62 #### .language(languages)
64 Return the first accepted language. If nothing in `languages` is accepted,
65 then `false` is returned.
69 Return the languages that the request accepts, in the order of the client's
70 preference (most preferred first).
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`
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`.
83 Return the types that the request accepts, in the order of the client's
84 preference (most preferred first).
88 ### Simple type negotiation
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
96 var accepts = require('accepts')
97 var http = require('http')
99 function app (req, res) {
100 var accept = accepts(req)
102 // the order of this list is significant; should be server preferred order
103 switch (accept.type(['json', 'html'])) {
105 res.setHeader('Content-Type', 'application/json')
106 res.write('{"hello":"world!"}')
109 res.setHeader('Content-Type', 'text/html')
110 res.write('<b>hello, world!</b>')
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!')
122 http.createServer(app).listen(3000)
125 You can test this out with the cURL program:
127 curl -I -H'Accept: text/html' http://localhost:3000/
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