Express.js req.accepts() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.accepts() checks the request’s Accept header and returns the best match from the types you can produce. Pass extensions like 'json' / 'html' or full MIME types like 'application/json'.

If nothing matches, it returns false — then you should usually send 406 Not Acceptable (or use a deliberate fallback). It runs inside your Express request handler on the server; browsers only send the Accept header.

Syntax

Call req.accepts in your Express.js app. Start with this line.

Express
req.accepts(types)
req.accepts(type1, type2, ...)
CallWhat it does
req.accepts(types)Primary signature.
req.accepts(type1, type2, ...)Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.accepts(types)
req.accepts(type1, type2, ...)

Parameters

ParameterDescription
typesRequired. One type, an array of types, or several string arguments. Use extensions (json) or MIME types (application/json).

Return Value

req.accepts(types) returns the best matching type string (same spelling you passed), or false when none of your types are acceptable.

Express
const type = req.accepts(['json', 'html']);
if (!type) return res.status(406).send('Not Acceptable');
// type === 'json' or 'html'

Express.js Examples

Example 1 — Choose HTML or JSON

Offer two formats and send the body that matches the client.

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

app.get('/hello', (req, res) => {
  const type = req.accepts(['html', 'json']);

  if (type === 'html') {
    return res.type('html').send('

Hello

'); } if (type === 'json') { return res.json({ message: 'Hello' }); } res.status(406).send('Not Acceptable'); }); app.listen(3000);

How It Works

  1. List what you support. ['html', 'json'] are extensions Express maps to MIME types.
  2. Negotiate once. req.accepts compares that list to the Accept header.
  3. Branch on the string. The return value is the same token you offered ('html' or 'json').
  4. Handle no match. false leads to 406 in this sample.

Example 2 — Quality Values Pick the Winner

When the client ranks types with q, the higher quality match wins even if you listed HTML first.

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

app.get('/data', (req, res) => {
  // Accept: text/*;q=0.5, application/json
  const type = req.accepts(['html', 'json']);
  // => 'json'  (json quality beats text/*)

  res.type(type || 'txt').send(String(type));
});

app.listen(3000);

How It Works

  1. Offer several types. The array order is your capability list, not a hard priority over q.
  2. Client ranks with q. application/json (default q=1) beats text/*;q=0.5.
  3. Best match is returned. Here that is 'json', even though 'html' was listed first.
  4. Same API for MIME strings. req.accepts('text/html') returns 'text/html' when that is the match.

Example 3 — No Match Means 406

When the client only accepts something you do not offer, return Not Acceptable.

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

app.get('/report', (req, res) => {
  const type = req.accepts('json', 'html');

  if (!type) {
    return res.status(406).send('Not Acceptable');
  }

  if (type === 'json') {
    return res.json({ ok: true });
  }
  res.type('html').send('

ok

'); }); app.listen(3000);

How It Works

  1. Pass types as arguments. req.accepts('json', 'html') is the same idea as an array.
  2. Check for false. Official docs recommend 406 when nothing matches.
  3. Only then send a body. Matching clients get JSON or HTML as usual.
  4. Raw header is different. req.get('Accept') returns the header text; req.accepts does the matching.

Runtime Support

req.accepts() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only send the Accept header.

✓ Server · Express 4 & 5

req.accepts()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.accepts() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It reads the request Accept header and returns the best match from the types you offer (extensions like json or full MIME types). If nothing matches, it returns false.
Respond with 406 Not Acceptable, or fall back to a default format if your API allows it.
A single type, several arguments (req.accepts('html', 'json')), or an array (req.accepts(['html', 'json'])). Extensions and full MIME types both work.
req.accepts() only picks the best type string. res.format() uses that negotiation and runs a handler map for each type (and can send 406 automatically).
No. It runs on your Node.js server. Browsers only send the Accept header on the HTTP request.

Next: req.acceptsCharsets()

Negotiate character encodings from the Accept-Charset header.

req.acceptsCharsets() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful