Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

accepts

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

accepts

Higher-level content negotiation

  • 1.3.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
32M
decreased by-5.2%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 02 Feb 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc