In Express.js, res.format() performs content negotiation on the request Accept header. It uses req.accepts() to pick the best matching handler from the object you pass, ordered by the client’s quality values.
Keys may be full MIME types (application/json) or short extension names (json, html, text). When a handler is selected, Express sets Content-Type (you can still change it with res.set() or res.type()). No match → 406, or your default callback.
Syntax
Call res.format in your Express.js app. Start with this line.
JSON matches first. Clients asking for JSON get the array payload.
No match → default. XML (and other types) hit the default callback.
You control the 406 body. Official sample logs and sends Not Acceptable.
Omit default only if you are fine with Express’s built-in 406 response.
Compatibility
Runtime Support
res.format() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Clients only send an Accept header; they do not execute this method.
✓ Server · Express 4 & 5
res.format()
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.
ServerNode.js runtime API
Node.js18+ for Express 5 · Express 4 supported
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.format()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Use curl -H "Accept: …" to try each format.
Frequently Asked Questions
It performs content negotiation on the request Accept header using req.accepts(), then runs the matching handler. You can map MIME types or short extension names to callbacks.
If the Accept header is not specified, Express invokes the first callback in the object you passed.
Without a default handler, the server responds with 406 Not Acceptable. With default, that callback runs instead.
Yes. Extension names such as text, html, and json are supported for a shorter map.
Yes — Content-Type is set when a callback is selected. You can still change it inside the handler with res.set() or res.type().