Express.js res.set() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.set() sets HTTP response headers on the outgoing reply. Pass a field name and value, or an object to set several headers at once. It is aliased as res.header().

Call it before methods that send the body (res.send(), res.json(), res.end()). After headers are flushed, changes no longer apply — check res.headersSent if you are unsure.

Syntax

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

Express
res.set(field, value)
res.set(headersObject)
res.header(field, value)   // alias
CallWhat it does
res.set(field, value)Primary signature.
res.set(headersObject)Related call form.
res.header(field, value) // aliasRelated 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.set(field, value)
res.set(headersObject)
res.header(field, value)   // alias

Parameters

ParameterDescription
fieldRequired. Header name, or an object of name/value pairs to set many headers at once.
valueRequired. Header value when field is a string. Use an array for multiple values of the same name.

Return Value

res.set() (alias res.header) returns res for chaining. Values may be a string or an array of strings for multi-value headers.

Express
res.set('Content-Type', 'text/plain')
  .set({ 'X-Request-Id': id })
  .send('ok');

Express.js Examples

Example 1 — Set Content-Type

Set one header, then send a plain text body.

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

app.get('/plain', (req, res) => {
  res.set('Content-Type', 'text/plain');
  res.send('hello');
});

app.listen(3000);

How It Works

  1. Name the field. 'Content-Type' is the header to write.
  2. Provide the value. 'text/plain' becomes that header’s value.
  3. Send afterward. res.send uses the headers already set.
  4. Same as res.header. You could write res.header('Content-Type', 'text/plain').

Example 2 — Set Several Headers at Once

Pass an object — the pattern shown in the Express docs.

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

app.get('/meta', (req, res) => {
  res.set({
    'Content-Type': 'text/plain',
    'Content-Length': '5',
    ETag: '12345'
  });
  res.send('hello');
});

app.listen(3000);

How It Works

  1. One call, many fields. Each object key is a header name.
  2. Values are assigned together. Useful for cache and type metadata.
  3. Still set before the body. Order matters relative to send.
  4. Overwrite on repeat. A later res.set('ETag', …) replaces 12345.

Example 3 — Chain Headers, Status, and Body

Use the return value of res.set() to keep the handler compact.

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

app.get('/api/ping', (req, res) => {
  res
    .set('X-Request-Id', 'demo-1')
    .status(200)
    .json({ ok: true });
});

app.listen(3000);

How It Works

  1. Set a custom header. X-Request-Id is available to clients and logs.
  2. Chain status. .status(200) uses the same res object.
  3. Finish with JSON. res.json also sets Content-Type for JSON.
  4. Headers first. The custom header is applied before the body is written.

Runtime Support

res.set() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the headers on the HTTP response.

✓ Server · Express 4 & 5

res.set()

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.set() 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 sets one HTTP response header to a value, or several headers at once when you pass an object. It returns res so you can chain other methods.
No. res.header() is an alias of res.set() and behaves the same way.
Before you send the body with res.send(), res.json(), res.end(), and similar methods. After headers are sent, changes are too late.
res.set() replaces the header value. res.append() adds another value. Calling res.set() after res.append() resets what append built.
No. It runs on your Node.js server. Browsers only receive the headers on the HTTP response.

Next: res.status()

Set the HTTP status code and chain send or json.

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