req.accepts()

Checks whether the request Accept header allows the given content types.

Since Express 4 Spec ↗

Syntax

req.accepts(types)

Parameters

NameTypeRequiredDescription
types string | string[] Yes One or more content types or extensions to test, in priority order.

Returns

string | false — The best matching type, or false if none acceptable.

Examples

app.get('/data', (req, res) => {
  switch (req.accepts(['html', 'json'])) {
    case 'json': return res.json({ ok: true });
    case 'html': return res.send('<p>ok</p>');
    default: return res.sendStatus(406);
  }
});
Output
$ curl -H 'Accept: application/json' localhost:3000/data
{"ok":true}
app.get('/x', (req, res) => {
  res.send(req.accepts('json') ? 'json ok' : 'no json');
});
Output
$ curl -H 'Accept: text/html' localhost:3000/x
no json

Notes

Drives content negotiation; respond with 406 Not Acceptable when it returns false. Related helpers: `req.acceptsCharsets`, `req.acceptsEncodings`, `req.acceptsLanguages`. `res.format()` is a higher-level alternative.

See also