accepts
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
mime-types | 2.1.35 | 5.46 kB | MIT | prod | |
negotiator | 0.6.1 | 6.47 kB | MIT | prod |
The "accepts" is a higher-level content negotiation tool based on the negotiator library. Extracted from the koa module for general use, it's designed to simplify the process of content negotiation with clients. It enhances the functionality of the negotiator by allowing types as an array or arguments list, shorthand type names like "json", treating non-existent headers as "*", and returning false when no types match.\
You can use the "accepts" module by installing it through the npm registry. After installation, require the module in your file and you can start using it. Here is an example:
$ npm install accepts
After installing the module you can use it as follows:
var accepts = require('accepts')
var http = require('http')
function app (req, res) {
var accept = accepts(req)
// the order of this list is significant; should be server preferred order
switch (accept.type(['json', 'html'])) {
case 'json':
res.setHeader('Content-Type', 'application/json')
res.write('{"hello":"world!"}')
break;
case 'html':
res.setHeader('Content-Type', 'text/html')
res.write('<b>hello, world!</b>')
break;
default:
// the fallback is text/plain, so no need to specify it above
res.setHeader('Content-Type', 'text/plain')
res.write('hello, world!')
break;
}
res.end()
}
http.createServer(app).listen(3000)
This script will handle responses based on the client's "Accept" header. It creates an HTTP server which listens on port 3000. Whenever a request comes in, it checks the "Accept" header of the incoming request and based on the content types that the client can handle, it sends a response in the most suitable format.
The documentation for the "accepts" module can be found in its README file on its GitHub repository: https://github.com/jshttp/accepts. The README includes a detailed API reference as well as usage examples.