Express.js res.format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

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.

Express
res.format(object)
res.format({
'text/plain'() { res.send('hey'); },
'text/html'() { res.send('<p>hey</p>'); },
'application/json'() { res.send({ message: 'hey' }); },
default() { res.status(406).send('Not Acceptable'); }
});
res.format({
text() { res.send('hey'); },
html() { res.send('<p>hey</p>'); },
json() { res.send({ message: 'hey' }); }
});
CallWhat it does
res.format(object)Primary signature.
res.format({Related call form.
'text/plain'() { res.send('hey'); },Related call form.
'text/html'() { res.send('<p>hey</p>'); },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();

res.format(object)
res.format({
'text/plain'() { res.send('hey'); },
'text/html'() { res.send('<p>hey</p>'); },
'application/json'() { res.send({ message: 'hey' }); },
default() { res.status(406).send('Not Acceptable'); }
});
res.format({
text() { res.send('hey'); },
html() { res.send('<p>hey</p>'); },
json() { res.send({ message: 'hey' }); }
});

Parameters

ParameterDescription
objectOptional. Map of MIME types or extension names → handler functions. Optional default key for unmatched requests.
MIME typeRequired. Canonical form from the docs
Extension nameRequired. Less verbose; maps to the same types
defaultRequired. Runs when nothing else matches (instead of auto 406)

Return Value

res.format() returns res after selecting a content negotiator callback. Your callbacks must send the response; format itself only dispatches.

Express
res.format({
  'text/plain'() { res.send('hi'); },
  'application/json'() { res.json({ ok: true }); },
  default() { res.status(406).send('Not Acceptable'); }
});

Express.js Examples

Example 1 — HTML, JSON, or Text from One Route

Same user data; response shape follows Accept.

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

app.get('/profile', (req, res) => {
  const user = { id: 1, name: 'Mari' };

  res.format({
    'text/plain'() {
      res.send('User: ' + user.name);
    },
    'text/html'() {
      res.send('<h1>' + user.name + '</h1>');
    },
    'application/json'() {
      res.json(user);
    }
  });
});

app.listen(3000);

How It Works

  1. One payload, three shapes. The route prepares user once.
  2. format picks a handler. req.accepts() matches against the keys.
  3. Content-Type is set for the selected type; then that callback sends the body.
  4. Only one handler runs. The others are skipped for that request.

Example 2 — Shorter Extension Names

Same idea with text / html / json keys from the official docs.

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

app.get('/hey', (req, res) => {
  res.format({
    text() {
      res.send('hey');
    },
    html() {
      res.send('<p>hey</p>');
    },
    json() {
      res.send({ message: 'hey' });
    }
  });
});

app.listen(3000);

How It Works

  1. Extension keys map to MIME types. json ↔ application/json, and so on.
  2. Less verbose map. Same negotiation rules as full MIME keys.
  3. */* uses the first key. Here that is text → plain hey.
  4. Missing Accept also runs the first callback, per the docs.

Example 3 — Default Handler for Unsupported Types

Return JSON when asked; otherwise send an explicit 406.

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

app.get('/data', (req, res) => {
  res.format({
    'application/json'() {
      res.json({ items: [1, 2, 3] });
    },
    default() {
      // log the request and respond with 406
      res.status(406).send('Not Acceptable');
    }
  });
});

app.listen(3000);

How It Works

  1. JSON matches first. Clients asking for JSON get the array payload.
  2. No match → default. XML (and other types) hit the default callback.
  3. You control the 406 body. Official sample logs and sends Not Acceptable.
  4. Omit default only if you are fine with Express’s built-in 406 response.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via 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().

Next: res.get()

Read a response header value you already set.

res.get() 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