Socket
Socket
Sign inDemoInstall

@tinyhttp/accepts

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tinyhttp/accepts

accepts rewrite in TypeScript


Version published
Maintainers
1
Created

What is @tinyhttp/accepts?

@tinyhttp/accepts is a package for handling HTTP content negotiation in Node.js applications. It helps in determining the best content type, language, encoding, and charset to respond with based on the client's request headers.

What are @tinyhttp/accepts's main functionalities?

Content Type Negotiation

This feature allows you to determine the best content type to respond with based on the client's Accept header. The code sample demonstrates how to respond with HTML, JSON, or plain text depending on what the client prefers.

const accepts = require('@tinyhttp/accepts');
const http = require('http');

http.createServer((req, res) => {
  const accept = accepts(req);
  const type = accept.type(['html', 'json', 'text']);

  if (type === 'html') {
    res.setHeader('Content-Type', 'text/html');
    res.end('<p>Hello, world!</p>');
  } else if (type === 'json') {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({ message: 'Hello, world!' }));
  } else {
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, world!');
  }
}).listen(3000);

Language Negotiation

This feature allows you to determine the best language to respond with based on the client's Accept-Language header. The code sample demonstrates how to respond in English, Spanish, or French depending on the client's preference.

const accepts = require('@tinyhttp/accepts');
const http = require('http');

http.createServer((req, res) => {
  const accept = accepts(req);
  const lang = accept.language(['en', 'es', 'fr']);

  if (lang === 'en') {
    res.end('Hello, world!');
  } else if (lang === 'es') {
    res.end('¡Hola, mundo!');
  } else if (lang === 'fr') {
    res.end('Bonjour, le monde!');
  } else {
    res.end('Hello, world!');
  }
}).listen(3000);

Encoding Negotiation

This feature allows you to determine the best encoding to respond with based on the client's Accept-Encoding header. The code sample demonstrates how to respond with gzip, deflate, or plain content depending on the client's preference.

const accepts = require('@tinyhttp/accepts');
const http = require('http');

http.createServer((req, res) => {
  const accept = accepts(req);
  const encoding = accept.encoding(['gzip', 'deflate', 'identity']);

  if (encoding === 'gzip') {
    res.setHeader('Content-Encoding', 'gzip');
    res.end('gzipped content');
  } else if (encoding === 'deflate') {
    res.setHeader('Content-Encoding', 'deflate');
    res.end('deflated content');
  } else {
    res.end('plain content');
  }
}).listen(3000);

Charset Negotiation

This feature allows you to determine the best charset to respond with based on the client's Accept-Charset header. The code sample demonstrates how to respond with utf-8, iso-8859-1, or windows-1252 charset depending on the client's preference.

const accepts = require('@tinyhttp/accepts');
const http = require('http');

http.createServer((req, res) => {
  const accept = accepts(req);
  const charset = accept.charset(['utf-8', 'iso-8859-1', 'windows-1252']);

  if (charset === 'utf-8') {
    res.setHeader('Content-Type', 'text/plain; charset=utf-8');
    res.end('Hello, world!');
  } else if (charset === 'iso-8859-1') {
    res.setHeader('Content-Type', 'text/plain; charset=iso-8859-1');
    res.end('Hello, world!');
  } else if (charset === 'windows-1252') {
    res.setHeader('Content-Type', 'text/plain; charset=windows-1252');
    res.end('Hello, world!');
  } else {
    res.end('Hello, world!');
  }
}).listen(3000);

Other packages similar to @tinyhttp/accepts

FAQs

Package last updated on 11 Sep 2024

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