Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

res.set

Sets one or more HTTP response headers. Use a field/value pair for one header or an object for several headers, and chain the returned response object.

How it works

Use res.set() in a route handler to assign HTTP response headers. Pass a field and value for one header, or pass an object containing several header-value pairs; the method returns the response object so calls can be chained.

Syntax

res.set(field [, val])

Parameters

NameTypeDescription
fieldstring | objectThe response header field name, or an object mapping header names to values.
valstringThe value for the selected response header field.

Returns

The response object.

Key points

  • The method assigns a named HTTP response header to the supplied value.
  • A header can be set with a field name followed by a string value.
  • An object lets one call configure multiple response headers.

Common mistakes

  • Calling res.set() after res.append() replaces the value previously added by res.append().

Example: Set response headers

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

app.get('/', (req, res) => {
  res.set('Cache-Control', 'no-store');
  res.set({
    'X-Content-Type-Options': 'nosniff',
    'Content-Type': 'text/plain; charset=utf-8'
  });
  res.send('Hello');
});

app.listen(3000);

Sets individual and multiple response headers before sending a plain-text response.

When to use it

Content type and length

Set content metadata before returning a body.

Cache-control policy

Apply browser or intermediary caching directives.

Custom response headers

Add application-specific metadata to an outgoing response.

Interview questions

QWhat does res.set() do, and what forms of input does it accept?

res.set() assigns headers on the outgoing HTTP response. It accepts either one header name with its value or an object containing multiple headers.

Follow-up: What happens if it follows res.append()? The set operation resets the previously appended header value.

QWhat is the signature of res.set()?

The single-header form is res.set(field, [val]), while the object form is res.set(field). The object form is useful when several headers must be configured together.

Follow-up: How can you set both cache policy and a custom header? Pass an object with both field-value pairs to res.set().

QWhen should res.set() be called in a route?

Headers should be configured before the response is completed, such as before calling res.send(). The method belongs to Express's response object and is used inside a request handler.

Follow-up: What should be checked when a header appears missing? Verify that the header was set before the response was sent and that a later operation did not replace it.

  • res.append()
  • res.get()
  • res.send()
  • res.type()

Source: Response. Checked 2026-10-15.