HTTP

response.headersSent

Indicates whether headers have been sent. Useful for diagnosing duplicate response paths; it is not proof that the body has completed or that the client has received it. Avoid using it as a substitute for clear branch ownership. Tested on Node.js 22.16.0. Example output (one line per entry): ["false", "true", "ab"].

Formal Syntax & Structure

nodejs syntaxnodejs
response.headersSent

Return Value

boolean

Whether headers have been sent.

Working Example

Example Snippetnodejs
import http from 'node:http';const server = http.createServer((req, res) => { console.log(res.headersSent); res.write('a'); console.log(res.headersSent); res.end('b'); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try {    console.log(await (await fetch(url)).text());}finally {    server.closeAllConnections();    await new Promise(r => server.close(r));}
Node.js Reference