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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With