HTTP

response.setHeader()

Stores a header for the outgoing response before commitment. A repeated call replaces the previous value for that name. Do not put untrusted or secret values into headers; some headers require multiple distinct values. Tested on Node.js 22.16.0. Example output (one line per entry): ["2 book"].

Formal Syntax & Structure

nodejs syntaxnodejs
response.setHeader(name, value)

Parameters & Attributes

NameTypeDescription
namestringHeader name.
valuenumber | string | string[]Header value.

Return Value

http.ServerResponse

The response instance.

Working Example

Example Snippetnodejs
import http from 'node:http';const server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/plain; charset=utf-8'); res.setHeader('X-Edition', '2'); res.end('book'); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try {    const r = await fetch(url);    console.log(r.headers.get('x-edition'), await r.text());}finally {    server.closeAllConnections();    await new Promise(r => server.close(r));}
Node.js Reference