Express.js res.vary() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.vary(field) adds a request header name to the response Vary header if it is not there already. Caches use Vary to know which request headers can change the representation of the same URL.

Call it when your reply depends on headers such as Accept, User-Agent, or Accept-Encoding. It returns res so you can chain res.render(), res.send(), and similar methods.

Syntax

Call res.vary in your Express.js app. Start with this line.

Express
res.vary(field)
CallWhat it does
res.vary(field)Primary signature.

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.vary(field)

Parameters

ParameterDescription
fieldRequired. Request header name to list in Vary (for example Accept or User-Agent).
AcceptRequired. Different body types (HTML vs JSON) for the same URL
User-AgentRequired. Different markup or assets by client (official example)
Accept-EncodingRequired. Compressed vs uncompressed bodies
Accept-LanguageRequired. Localized content for the same path

Return Value

res.vary(field) returns res for chaining after updating the Vary header. It does not send a body.

Express
res.vary('Accept')
  .format({
    'text/html'() { res.send('

hi

'); }, 'application/json'() { res.json({ ok: true }); } });

Express.js Examples

Example 1 — Official Pattern with render

Docs example: mark User-Agent as a varying factor, then render a view.

Express
const express = require('express');
const app = express();
app.set('view engine', 'ejs');

app.get('/docs', (req, res) => {
  res.vary('User-Agent').render('docs');
});

app.listen(3000);

How It Works

  1. Name the header. User-Agent is what may change the page.
  2. Update Vary. Caches store separate variants per User-Agent when configured to honor Vary.
  3. Chain render. vary returns res, so .render('docs') follows.
  4. Same URL, different clients. Desktop and mobile can be cached correctly.

Example 2 — Vary on Accept

When content negotiation picks HTML or JSON, tell caches about Accept.

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

app.get('/message', (req, res) => {
  res.vary('Accept');
  const type = req.accepts(['html', 'json']);
  if (type === 'json') {
    return res.json({ message: 'hey' });
  }
  res.type('html').send('<p>hey</p>');
});

app.listen(3000);

How It Works

  1. Declare Accept. The response depends on the client’s Accept header.
  2. Negotiate. req.accepts picks json or html.
  3. Send the match. JSON or HTML for the same path.
  4. Caches stay safe. Vary prevents serving HTML to a JSON client from cache.

Example 3 — Multiple Fields, No Duplicates

Call vary more than once; a repeated field is skipped.

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

app.get('/api/item', (req, res) => {
  res
    .vary('Accept')
    .vary('Accept-Encoding')
    .vary('Accept') // already listed — ignored
    .json({ id: 1 });
});

app.listen(3000);

How It Works

  1. First call. Adds Accept to Vary.
  2. Second call. Adds Accept-Encoding.
  3. Third call. Accept is already present, so nothing changes.
  4. Finish with JSON. Body and headers go out together.

Runtime Support

res.vary() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Caches and browsers only see the Vary header on the HTTP response.

✓ Server · Express 4 & 5

res.vary()

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
res.vary() 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 adds a request header name to the Vary response header if that name is not already listed. Caches use Vary to know which request headers can change the response.
No. It only updates the Vary header. You still finish with res.send(), res.json(), res.render(), or another method.
Yes. Each call adds a field only if it is missing, so duplicates are avoided.
When the same URL can return different representations based on Accept — for example HTML vs JSON via res.format() or req.accepts().
No. It runs on your Node.js server. Caches and browsers only see the Vary header on the HTTP response.

Next: Router

Continue with the Express Router object and modular routes.

Router overview →

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