| 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 |
|
|---|
| 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 | ```js
|
|---|
| 33 | var accepts = require('accepts')
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | ### accepts(req)
|
|---|
| 37 |
|
|---|
| 38 | Create a new `Accepts` object for the given `req`.
|
|---|
| 39 |
|
|---|
| 40 | #### .charset(charsets)
|
|---|
| 41 |
|
|---|
| 42 | Return the first accepted charset. If nothing in `charsets` is accepted,
|
|---|
| 43 | then `false` is returned.
|
|---|
| 44 |
|
|---|
| 45 | #### .charsets()
|
|---|
| 46 |
|
|---|
| 47 | Return the charsets that the request accepts, in the order of the client's
|
|---|
| 48 | preference (most preferred first).
|
|---|
| 49 |
|
|---|
| 50 | #### .encoding(encodings)
|
|---|
| 51 |
|
|---|
| 52 | Return the first accepted encoding. If nothing in `encodings` is accepted,
|
|---|
| 53 | then `false` is returned.
|
|---|
| 54 |
|
|---|
| 55 | #### .encodings()
|
|---|
| 56 |
|
|---|
| 57 | Return the encodings that the request accepts, in the order of the client's
|
|---|
| 58 | preference (most preferred first).
|
|---|
| 59 |
|
|---|
| 60 | #### .language(languages)
|
|---|
| 61 |
|
|---|
| 62 | Return the first accepted language. If nothing in `languages` is accepted,
|
|---|
| 63 | then `false` is returned.
|
|---|
| 64 |
|
|---|
| 65 | #### .languages()
|
|---|
| 66 |
|
|---|
| 67 | Return the languages that the request accepts, in the order of the client's
|
|---|
| 68 | preference (most preferred first).
|
|---|
| 69 |
|
|---|
| 70 | #### .type(types)
|
|---|
| 71 |
|
|---|
| 72 | Return the first accepted type (and it is returned as the same text as what
|
|---|
| 73 | appears in the `types` array). If nothing in `types` is accepted, then `false`
|
|---|
| 74 | is returned.
|
|---|
| 75 |
|
|---|
| 76 | The `types` array can contain full MIME types or file extensions. Any value
|
|---|
| 77 | that is not a full MIME types is passed to `require('mime-types').lookup`.
|
|---|
| 78 |
|
|---|
| 79 | #### .types()
|
|---|
| 80 |
|
|---|
| 81 | Return the types that the request accepts, in the order of the client's
|
|---|
| 82 | preference (most preferred first).
|
|---|
| 83 |
|
|---|
| 84 | ## Examples
|
|---|
| 85 |
|
|---|
| 86 | ### Simple type negotiation
|
|---|
| 87 |
|
|---|
| 88 | This simple example shows how to use `accepts` to return a different typed
|
|---|
| 89 | respond body based on what the client wants to accept. The server lists it's
|
|---|
| 90 | preferences in order and will get back the best match between the client and
|
|---|
| 91 | server.
|
|---|
| 92 |
|
|---|
| 93 | ```js
|
|---|
| 94 | var accepts = require('accepts')
|
|---|
| 95 | var http = require('http')
|
|---|
| 96 |
|
|---|
| 97 | function 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 |
|
|---|
| 120 | http.createServer(app).listen(3000)
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | You can test this out with the cURL program:
|
|---|
| 124 | ```sh
|
|---|
| 125 | curl -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
|
|---|