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
- A request fails but your Express error-handling middleware never runs. How do you work out why?mediumAlso on express and error-handling4 min
- Walk me through the middleware chain of an Express service. What has to come before what, and why?mediumAlso on express and middleware4 min
- A promise rejects and nothing is awaiting it. What does Node do?hardAlso on error-handling and nodejs4 min
- How do you design the error contract for an API that other teams have to program against?hardAlso on error-handling and http6 min