I am writing a library which may set headers. I want to give a custom error message if headers have already been sent, instead of just letting it fail with the "Can't set headers after they are sent" message given by Node.js. So how to check if headers have already been sent?

- 41,009
- 21
- 145
- 105

- 10,523
- 20
- 63
- 93
-
2powerboy would you be able to accept the other answer, because the currently accepted answer doesn't work anymore? See @fiatjaf 's comment. – Willem Mulder Oct 31 '15 at 20:54
3 Answers
Node supports the res.headersSent
these days, so you could/should use that. It is a read-only boolean indicating whether the headers have already been sent.
if(res.headersSent) { ... }
See http://nodejs.org/api/http.html#http_response_headerssent
Note: this is the preferred way of doing it, compared to the older Connect 'headerSent' property that Niko mentions.

- 12,974
- 3
- 37
- 62
-
7This should be made the accepted answer seeing as the current accepted answer no longer works. – mantagen Jun 27 '17 at 18:07
EDIT: as of express 4.x, you need to use res.headersSent. Note also that you may want to use setTimeout before checking, as it isn't set to true immediately following a call to res.send(). Source
Simple: Connect's Response class provides a public property "headerSent".
res.headerSent
is a boolean value that indicates whether the headers have already been sent to the client.
From the source code:
/**
* Provide a public "header sent" flag
* until node does.
*
* @return {Boolean}
* @api public
*/
res.__defineGetter__('headerSent', function(){
return this._header;
});
https://github.com/senchalabs/connect/blob/master/lib/patch.js#L22

- 2,807
- 2
- 29
- 38

- 26,516
- 9
- 93
- 110
-
1Note that this is not the same as http://nodejs.org/docs/latest/api/http.html#http_response_headerssent In express it seems to return the headers sent.. rather than a bool of if the headers have been sent. – Lee Olayvar Apr 20 '13 at 18:47
-
-
6@powerboy Node currently supports res.headersSent natively, so it's probably a good idea to to start using that property. – Willem Mulder Jun 06 '14 at 08:47
-
11As of May 17, 2014, res.headerSent has been deprecated by senchalabs/connect in favour of res.headersSent. – Kunal Kapadia Nov 28 '14 at 12:43
-
11
-
I've edited the answer to reflect the headersSent thing, as it is accepted but is now misleading. – MalcolmOcean Apr 17 '18 at 18:09
-
-
Seems it doesn't work to just use zero... I have mine set to 250ms to be safe, but fewer would probably be fine. – MalcolmOcean May 07 '18 at 23:16
-
i flagged this answer for moderation. `res.headersSent` answer should be the accepted solution as this one no longer works. – Mark Shust at M.academy Jun 12 '19 at 14:06
-
Others answers point to Node.js or Github websites.
Below is from Expressjs website: https://expressjs.com/en/api.html#res.headersSent
app.get('/', function (req, res) {
console.log(res.headersSent); // false
res.send('OK');
console.log(res.headersSent); // true
});

- 25,399
- 9
- 157
- 140