Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

req.accepts

req.accepts() checks candidate response types against the request's Accept header and returns the best match. It returns false when none of the supplied types is acceptable.

How it works

Call req.accepts() with one or more candidate content types. Express compares them with the request's Accept header and returns the best matching candidate; when nothing matches, it returns false so the application can send a 406 response.

Syntax

req.accepts(types)

Parameters

NameTypeDescription
typesString | String[]The content type or types to test: a MIME type, extension name, comma-delimited list, or array.

Returns

String | false

Key points

  • Use it for content negotiation: it determines whether requested representations fit the client's Accept header.
  • With several candidates, the return value identifies the preferred acceptable representation rather than merely reporting true.
  • Candidates can be written as full MIME types, which is useful when precise media-type matching matters.
  • Short extensions such as json are supported, making route negotiation concise.
  • Multiple alternatives may be supplied either in one comma-separated string or as an array.
  • The selected result is the candidate value that matched, so callers can branch directly on it.

Common mistakes

  • Test the return value explicitly; a non-match is false, not a thrown error or an empty string.
  • When no representation is supported, use HTTP 406 rather than silently selecting an unrelated format.

Example: Choose a response format from Accept

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  const match = req.accepts(['json', 'html']);
  if (match === 'json') return res.json({ message: 'Hello' });
  if (match === 'html') return res.type('html').send('<h1>Hello</h1>');
  res.status(406).send('Not Acceptable');
});

app.listen(3000);

Run the server, then request / with an Accept header such as application/json or text/html.

When to use it

Content negotiation

A route supports JSON and HTML and needs to select the representation requested by the client.

406 fallback handling

An endpoint should reject requests that cannot accept any supported response format.

Extension-based matching

A handler wants concise candidate names instead of repeating full MIME strings.

Interview questions

QWhat does req.accepts() return?

It checks candidate MIME types or extensions against the request's Accept header and returns the best match. If no candidate is acceptable, it returns false.

Follow-up: How would you handle a client that accepts both JSON and HTML? Pass both candidates as an array and branch on the returned best match.

QWhat forms can the types argument take?

The argument may be a MIME type, an extension, a comma-delimited list, or an array. For multiple candidates, Express chooses the best acceptable match.

Follow-up: Why might an array be preferable to a comma-delimited string? It keeps candidate values structurally separate and is easier to build dynamically.

QHow does the Accept header influence the result?

The method bases its decision on the request's Accept HTTP header. A wildcard or quality-weighted header can affect which candidate is selected.

Follow-up: What should the route do when the header excludes every candidate? Return a 406 response because no requested representation can be served.

  • req.acceptsCharsets()
  • req.acceptsEncodings()
  • req.acceptsLanguages()
  • req.get()
  • res.format()

Source: Request Object - Express.js 4.x. Checked 2026-10-15.