accepts
Last updated
Last updated
Higher level content negotiation based on negotiator. Extracted from koa for general use.
In addition to negotiator, it allows:
Allows types as an array or arguments list, ie (['text/html', 'application/json'])
as well as ('text/html', 'application/json')
.
Allows type shorthands such as json
.
Returns false
when no types match
Treats non-existent headers as *
This is a Node.js module available through the npm registry. Installation is done using the npm install
command:
Create a new Accepts
object for the given req
.
Return the first accepted charset. If nothing in charsets
is accepted, then false
is returned.
Return the charsets that the request accepts, in the order of the client's preference (most preferred first).
Return the first accepted encoding. If nothing in encodings
is accepted, then false
is returned.
Return the encodings that the request accepts, in the order of the client's preference (most preferred first).
Return the first accepted language. If nothing in languages
is accepted, then false
is returned.
Return the languages that the request accepts, in the order of the client's preference (most preferred first).
Return the first accepted type (and it is returned as the same text as what appears in the types
array). If nothing in types
is accepted, then false
is returned.
The types
array can contain full MIME types or file extensions. Any value that is not a full MIME types is passed to require('mime-types').lookup
.
Return the types that the request accepts, in the order of the client's preference (most preferred first).
This simple example shows how to use accepts
to return a different typed respond body based on what the client wants to accept. The server lists it's preferences in order and will get back the best match between the client and server.
You can test this out with the cURL program: