Skip to content
Preptima
mediumCodingConceptMidSenior

Your logs are full of Cannot set headers after they are sent. Walk me through how a request gets into that state.

ERR_HTTP_HEADERS_SENT means an Express route tried to set status or headers after the response had already been sent. The usual cause is two response paths: missing return after res.send, async callbacks racing, or an error handler trying to respond after headers are committed.

4 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup

Answer

Short answer

In Express, Cannot set headers after they are sent means the request already flushed a response and later code tried to set headers or status again. Look for missing return after res.send, double callback execution, async work that responds twice, or error middleware running after the response was committed.

Was this answer helpful?

Likely follow-ups

  • A timeout middleware replies at 2 seconds and the real handler resolves at 3. How do you make the late reply impossible rather than merely logged?
  • Where does a response-time header have to be set on a streamed response, and how do you record the duration if you cannot set one?
  • What does res.end do that res.send does not, and when does the difference matter?
  • How would you detect double responses in production before a user reports a truncated payload?

Related questions

Further reading