Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js - How to check if headers have already been sent?

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?

like image 702
powerboy Avatar asked Aug 19 '12 21:08

powerboy


1 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.

like image 153
Willem Mulder Avatar answered Oct 20 '22 04:10

Willem Mulder