What is negotiator?
The negotiator npm package is a content negotiation library used to parse and match HTTP headers for various content types. It allows servers to select the best representation of a resource based on the client's capabilities and preferences, as expressed through HTTP headers.
What are negotiator's main functionalities?
Content-Type Negotiation
This feature allows the server to determine the best media type to respond with, based on the client's 'Accept' HTTP header.
const Negotiator = require('negotiator');
const availableMediaTypes = ['text/html', 'application/json'];
const negotiator = new Negotiator(request);
const bestMediaType = negotiator.mediaType(availableMediaTypes);
Language Negotiation
This feature enables the server to select the best language for the response content based on the client's 'Accept-Language' HTTP header.
const Negotiator = require('negotiator');
const availableLanguages = ['en', 'es', 'fr'];
const negotiator = new Negotiator(request);
const bestLanguage = negotiator.language(availableLanguages);
Encoding Negotiation
This feature allows the server to choose the best encoding for the response based on the client's 'Accept-Encoding' HTTP header.
const Negotiator = require('negotiator');
const availableEncodings = ['gzip', 'deflate'];
const negotiator = new Negotiator(request);
const bestEncoding = negotiator.encoding(availableEncodings);
Charset Negotiation
This feature allows the server to determine the best charset for the response content based on the client's 'Accept-Charset' HTTP header.
const Negotiator = require('negotiator');
const availableCharsets = ['utf-8', 'iso-8859-1'];
const negotiator = new Negotiator(request);
const bestCharset = negotiator.charset(availableCharsets);
Other packages similar to negotiator
accepts
The 'accepts' npm package is similar to 'negotiator' in that it also provides content negotiation capabilities. It is built on top of 'negotiator' and provides a higher-level API for handling 'Accept', 'Accept-Encoding', 'Accept-Language', and 'Accept-Charset' headers.
negotiator
An HTTP content negotiator for Node.js
Installation
$ npm install negotiator
API
var Negotiator = require('negotiator')
Accept Negotiation
availableMediaTypes = ['text/html', 'text/plain', 'application/json']
negotiator = new Negotiator(request)
negotiator.mediaTypes()
negotiator.mediaTypes(availableMediaTypes)
negotiator.mediaType(availableMediaTypes)
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
negotiator = new Negotiator(request)
availableLanguages = ['en', 'es', 'fr']
negotiator.languages()
negotiator.languages(availableLanguages)
language = negotiator.language(availableLanguages)
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
availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
negotiator = new Negotiator(request)
negotiator.charsets()
negotiator.charsets(availableCharsets)
negotiator.charset(availableCharsets)
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
availableEncodings = ['identity', 'gzip']
negotiator = new Negotiator(request)
negotiator.encodings()
negotiator.encodings(availableEncodings)
negotiator.encoding(availableEncodings)
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.
encoding(availableEncodings, { preferred })
Returns the most preferred encoding from a list of available encodings, while prioritizing based on preferred
array between same-quality 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.
encodings(availableEncodings, { preferred })
Returns an array of preferred encodings ordered by priority from a list of
available encodings, while prioritizing based on preferred
array between same-quality encodings.
See Also
The accepts module builds on
this module and provides an alternative interface, mime type validation,
and more.
License
MIT