Is There a Way to Check Sent Headers with Node/ Express 2.x?


Is there a way of checking what specific headers have been sent using node/ express 2.x?

I have a file download that works perfectly most of the time, but in a few specific instances I get an error in Chrome (no errors in node):

Duplicate headers received from server
The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue.
Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple distinct Content-Disposition headers received. This is disallowed to protect against HTTP response splitting attacks.

For testing purposes, I'd like to see if a specific header has been sent or not, is there a way of doing this with node.js?

...And because someone's going to ask me about the code I use to set headers, I'm piping a stream as the download and are only setting headers in one spot.

res.setHeader('Content-disposition', 'attachment; filename=' + filename)
res.setHeader('Content-Length', stats.size)
res.setHeader('Content-type', 'application/pdf')

Problem courtesy of: Dropped.on.Caprica


The HTTP response is a WritableStream. When the stream closes, a finish event is emitted. Thus listening to it does the trick:

res.on('finish', function() {

Much more flexible. Can be put in a middleware or a resource handler.

Solution courtesy of: Mike Gleason jr Couturier


res.set("Content-disposition", "attachment; filename=\"""\"")

This worked for me.

Discussion courtesy of: Yogesh Agrawal

As @generalhenry stated on my question comments:

stream.pipe(res).on('end', function () {

The above line worked for me.

Discussion courtesy of: Dropped.on.Caprica

This recipe can be found in it's original form on Stack Over Flow.