Express.js res.headersSent Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.headersSent is a Boolean that tells you whether the app has already sent HTTP headers for this response. It is false before the first flush, and true afterward.

Use it as a guard in callbacks, downloads, and error handlers: if headers are already out, do not call res.send(), res.json(), or change status again. Prefer clear return after a successful send so only one path finishes the response.

Syntax

Read res.headersSent in your Express.js route handler. Start with this line.

Express
res.headersSent  // Boolean
app.get('/', (req, res) => {
console.log(res.headersSent); // false
res.send('OK');
console.log(res.headersSent); // true
});
if (!res.headersSent) {
res.status(500).send('Something went wrong');
}
CallWhat it does
res.headersSent // BooleanPrimary signature.
app.get('/', (req, res) => {Related call form.
console.log(res.headersSent); // falseRelated call form.
res.send('OK');Related 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.headersSent  // Boolean
app.get('/', (req, res) => {
console.log(res.headersSent); // false
res.send('OK');
console.log(res.headersSent); // true
});
if (!res.headersSent) {
res.status(500).send('Something went wrong');
}

Parameters

ParameterDescription
res.headersSentRequired. false until headers are sent; true once they have been flushed for this response.

Return Value

res.headersSent is a Boolean property that becomes true after response headers have been written. It is not a method — do not call it as a function.

Express
if (res.headersSent) {
  return next(err); // cannot send a new body safely
}
res.status(500).send('Error');

Express.js Examples

Example 1 — Watch the Flag Flip (Official Pattern)

Log headersSent before and after res.send, matching the Express docs.

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

app.get('/', (req, res) => {
  console.log(res.headersSent); // false
  res.send('OK');
  console.log(res.headersSent); // true

  if (!res.headersSent) {
    res.send('Fallback'); // does not run
  }
});

app.listen(3000);

How It Works

  1. Before send: headers are not flushed → false.
  2. res.send('OK') writes headers and body → flag becomes true.
  3. Guard sees true. The fallback send is skipped.
  4. Matches the docs. Same false → send → true sequence as the official example.

Example 2 — Guard a Download Error Callback

If transfer fails and nothing was sent yet, return 404. If headers already went out, skip a second body.

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

app.get('/file', (req, res) => {
  const filePath = path.join(__dirname, 'files', 'manual.pdf');

  res.download(filePath, (err) => {
    if (err) {
      // Response may be partially sent — check headersSent
      if (!res.headersSent) {
        res.status(404).send('File not found');
      }
      return;
    }
    console.log('Download completed');
  });
});

app.listen(3000);

How It Works

  1. download may fail late. Bytes or headers might already be on the wire.
  2. Official advice: keep partial sends in mind and check headersSent.
  3. false → safe 404. You can still start a normal error response.
  4. true → stop. Do not call status().send() again.

Example 3 — Central Error Handler Guard

If the response already started, pass the error onward instead of writing JSON.

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

app.get('/boom', (req, res, next) => {
  next(new Error('Something broke'));
});

app.use((err, req, res, next) => {
  if (res.headersSent) {
    return next(err);
  }
  res.status(500).json({ error: 'Internal Server Error' });
});

app.listen(3000);

How It Works

  1. Error middleware is last-resort. It may run after a partial response.
  2. Check first. If headers are out, delegate with next(err).
  3. Otherwise send JSON. Safe 500 when the response is still open.
  4. Avoids double-send in apps with streams, timeouts, or multiple async paths.

Runtime Support

res.headersSent is a Boolean on the Express response (Node HTTP response state). It runs on the server (Express 5 needs Node.js 18+). Browsers only see the finished response.

✓ Server · Express 4 & 5

res.headersSent

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 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.headersSent Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Log the flag before and after res.send to see it flip.

Frequently Asked Questions

It is a Boolean property that is true after the app has sent HTTP headers for the response, and false before that.
After methods that flush the response start sending — for example res.send(), res.json(), res.end(), or when a stream/file transfer begins writing headers.
To avoid writing a second response (status, headers, or body) after the first one started — the classic Cannot set headers after they are sent error.
No. It is a property. Read it with res.headersSent; do not call it like a function.
It is a safety check. You should still use clear control flow (return after send) so only one path finishes the response.

Next: res.json()

Send a JSON response with the correct Content-Type.

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