.. | ||
lib | ||
HISTORY.md | ||
index.js | ||
LICENSE | ||
package.json | ||
README.md |
negotiator
An HTTP content negotiator for Node.js
Installation
$ npm install negotiator
API
var Negotiator = require('negotiator')
Accept Negotiation
= ['text/html', 'text/plain', 'application/json']
availableMediaTypes
// The negotiator constructor receives a request object
= new Negotiator(request)
negotiator
// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
.mediaTypes()
negotiator// -> ['text/html', 'image/jpeg', 'application/*']
.mediaTypes(availableMediaTypes)
negotiator// -> ['text/html', 'application/json']
.mediaType(availableMediaTypes)
negotiator// -> 'text/html'
You can check a working example at
examples/accept.js
.
Methods
mediaType()
Returns the most preferred media type from the client.
mediaType(availableMediaType)
Returns the most preferred media type from a list of available media types.
mediaTypes()
Returns an array of preferred media types ordered by the client preference.
mediaTypes(availableMediaTypes)
Returns an array of preferred media types ordered by priority from a list of available media types.
Accept-Language Negotiation
= new Negotiator(request)
negotiator
= ['en', 'es', 'fr']
availableLanguages
// Let's say Accept-Language header is 'en;q=0.8, es, pt'
.languages()
negotiator// -> ['es', 'pt', 'en']
.languages(availableLanguages)
negotiator// -> ['es', 'en']
= negotiator.language(availableLanguages)
language // -> 'es'
You can check a working example at
examples/language.js
.
Methods
language()
Returns the most preferred language from the client.
language(availableLanguages)
Returns the most preferred language from a list of available languages.
languages()
Returns an array of preferred languages ordered by the client preference.
languages(availableLanguages)
Returns an array of preferred languages ordered by priority from a list of available languages.
Accept-Charset Negotiation
= ['utf-8', 'iso-8859-1', 'iso-8859-5']
availableCharsets
= new Negotiator(request)
negotiator
// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
.charsets()
negotiator// -> ['utf-8', 'iso-8859-1', 'utf-7']
.charsets(availableCharsets)
negotiator// -> ['utf-8', 'iso-8859-1']
.charset(availableCharsets)
negotiator// -> 'utf-8'
You can check a working example at
examples/charset.js
.
Methods
charset()
Returns the most preferred charset from the client.
charset(availableCharsets)
Returns the most preferred charset from a list of available charsets.
charsets()
Returns an array of preferred charsets ordered by the client preference.
charsets(availableCharsets)
Returns an array of preferred charsets ordered by priority from a list of available charsets.
Accept-Encoding Negotiation
= ['identity', 'gzip']
availableEncodings
= new Negotiator(request)
negotiator
// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
.encodings()
negotiator// -> ['gzip', 'identity', 'compress']
.encodings(availableEncodings)
negotiator// -> ['gzip', 'identity']
.encoding(availableEncodings)
negotiator// -> 'gzip'
You can check a working example at
examples/encoding.js
.
Methods
encoding()
Returns the most preferred encoding from the client.
encoding(availableEncodings)
Returns the most preferred encoding from a list of available encodings.
encodings()
Returns an array of preferred encodings ordered by the client preference.
encodings(availableEncodings)
Returns an array of preferred encodings ordered by priority from a list of available encodings.
See Also
The accepts module builds on this module and provides an alternative interface, mime type validation, and more.