negotiator
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Negotiator is an HTTP content negotiation module for Node.js. It aids your applications by simplifying the negotiation process between the server and client for specific content types, languages, charsets, and encodings that are based on HTTP headers.
To use Negotiator in your Node.js application, start by installing the negotiator package via npm using the command npm install negotiator
. Once installed, you can import Negotiator into your project using var Negotiator = require('negotiator')
.
Here's an example of how Negotiator can be used for media negotiation:
var Negotiator = require('negotiator');
var availableMediaTypes = ['text/html', 'text/plain', 'application/json'];
var negotiator = new Negotiator(request);
var preferredMediaType = negotiator.mediaType(availableMediaTypes);
In the above case, the preferred media type is determined based on the client's Accept header. The available media types are listed in the server, and the most preferred one is returned by the negotiator.mediaType(availableMediaTypes)
method.
The same method applies to the Accept-Language, Accept-Charset, and Accept-Encoding negotiations. You just need to use the appropriate methods such as negotiator.languages(availableLanguages)
, negotiator.charsets(availableCharsets)
, or negotiator.encodings(availableEncodings)
.
The documentation for the Negotiator module can be found in the readme file on the Github repository for the package, which is accessible at the following URL: https://github.com/jshttp/negotiator. The readme contains all needed information on how to install and utilize Negotiator in your Node.js project. It also provides details on the different methods available and illustrated examples of how to implement content negotiations using the module.