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)
Call
What 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.
GET /api/item
Vary: Accept, Accept-Encoding
Body: { "id": 1 }
How It Works
First call. Adds Accept to Vary.
Second call. Adds Accept-Encoding.
Third call.Accept is already present, so nothing changes.
Finish with JSON. Body and headers go out together.
Compatibility
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.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia 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.