Socket
Socket
Sign inDemoInstall

accepts

Package Overview
Dependencies
2
Maintainers
7
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    accepts

Higher-level content negotiation


Version published
Weekly downloads
30M
decreased by-1.19%
Maintainers
7
Install size
106 kB
Created
Weekly downloads
 

Package description

What is accepts?

The 'accepts' npm package is a utility for content negotiation in Node.js. It allows a server to interpret the content types that a client can handle and respond with the most appropriate content type. It is commonly used in HTTP server frameworks like Express to simplify the process of determining what MIME types the client accepts in the 'Accept' HTTP header.

What are accepts's main functionalities?

Content Type Negotiation

This code demonstrates how to use the 'accepts' package to determine the best response content type based on the client's 'Accept' header. The server responds with either JSON, HTML, or plain text.

const accepts = require('accepts');
const http = require('http');

http.createServer(function (req, res) {
  var accept = accepts(req);
  var preferredType = accept.type(['json', 'html']);
  if (preferredType === 'json') {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({ message: 'Hello, JSON!' }));
  } else if (preferredType === 'html') {
    res.setHeader('Content-Type', 'text/html');
    res.end('<p>Hello, HTML!</p>');
  } else {
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, plain text!');
  }
}).listen(3000);

Language Negotiation

This code snippet shows how to use the 'accepts' package to determine the client's preferred language from the 'Accept-Language' header and respond accordingly.

const accepts = require('accepts');
const http = require('http');

http.createServer(function (req, res) {
  var accept = accepts(req);
  var preferredLanguage = accept.language(['en', 'es', 'fr']);
  res.end('Preferred language: ' + preferredLanguage);
}).listen(3000);

Encoding Negotiation

This example illustrates how to use the 'accepts' package to negotiate the content encoding that the client supports, such as 'gzip' or 'deflate'.

const accepts = require('accepts');
const http = require('http');

http.createServer(function (req, res) {
  var accept = accepts(req);
  var preferredEncoding = accept.encoding(['gzip', 'deflate']);
  res.end('Preferred encoding: ' + preferredEncoding);
}).listen(3000);

Charset Negotiation

This code sample demonstrates how to use the 'accepts' package to determine which charset the client prefers, such as 'utf-8' or 'iso-8859-1'.

const accepts = require('accepts');
const http = require('http');

http.createServer(function (req, res) {
  var accept = accepts(req);
  var preferredCharset = accept.charset(['utf-8', 'iso-8859-1']);
  res.end('Preferred charset: ' + preferredCharset);
}).listen(3000);

Other packages similar to accepts

Readme

Source

Accepts

NPM version Build Status Coverage Status

Higher level content negotation based on negotiator. Extracted from koa for general use.

In addition to negotatior, 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 *

API

var accept = new Accepts(req)

var accepts = require('accepts')

http.createServer(function (req, res) {
  var accept = accepts(req)
})

accept[property]()

Returns all the explicitly accepted content property as an array in descending priority.

  • accept.types()
  • accept.encodings()
  • accept.charsets()
  • accept.languages()

They are also aliased in singular form such as accept.type(). accept.languages() is also aliased as accept.langs(), etc.

Note: you should almost never do this in a real app as it defeats the purpose of content negotiation.

Example:

// in Google Chrome
var encodings = accept.encodings() // -> ['sdch', 'gzip', 'deflate']

Since you probably don't support sdch, you should just supply the encodings you support:

var encoding = accept.encodings('gzip', 'deflate') // -> 'gzip', probably

accept[property](values, ...)

You can either have values be an array or have an argument list of values.

If the client does not accept any values, false will be returned. If the client accepts any values, the preferred value will be return.

For accept.types(), shorthand mime types are allowed.

Example:

// req.headers.accept = 'application/json'

accept.types('json') // -> 'json'
accept.types('html', 'json') // -> 'json'
accept.types('html') // -> false

// req.headers.accept = ''
// which is equivalent to `*`

accept.types() // -> [], no explicit types
accept.types('text/html', 'text/json') // -> 'text/html', since it was first

License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Last updated on 04 Jul 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc