Force flush headers using the HTTP module for NodeJS

21 Oct 2013 in Tech

Although it's not something that you'll commonly want to do, today I found myself wanting to flush HTTP headers immediately without sending any additional data with them. Internally, NodeJS won't send headers until it needs to. For most applications, this is when the first bit of data is sent.

After judicious use of grep, I came across the HTTP outgoing lib file, which contained all the information I needed to force node to flush the headers. You need to use writeHead to build up your headers, and then you can access the header string in response._header. Then, you write it to the socket using response.socket.write (or if you prefer, you can use response._writeRaw), and finally you mark that headers have already been sent so that if anything uses response.write in the future you won't get the headers sent twice (which gives the following, awesome error message: "Received problem 2 in the chunky parser")

So, the actual code:

// This will build the HTTP response to send back headers
response.writeHead(200, {
Connection: "keep-alive",
"Content-Type": "application/json; charset=utf-8",
"Transfer-Encoding": "chunked",
// Write the headers to the socket
// Mark the headers as sent
response._headerSent = true;