http.ServerResponse

The response object passed to HTTP server handlers - a writable stream.

Since Node 0.x Spec ↗

Syntax

(req, res) => { res.writeHead(...); res.end(...) }

Returns

Writable — A writable stream with writeHead, setHeader, statusCode, write, and end.

Examples

import { createServer } from 'node:http';

createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ ok: true }));
}).listen(3000);
Output
{"ok":true}
createServer((req, res) => {
  res.statusCode = 302;
  res.setHeader('Location', '/login');
  res.end();
}).listen(3000);
Output
HTTP/1.1 302 Found
Location: /login

Notes

Headers must be set before `writeHead` or the first `write`; setting them after throws `ERR_HTTP_HEADERS_SENT`. Every code path must call `res.end()` exactly once. `res.end(data)` is shorthand for write then end.

See also