I'm using express as my server and request to retrieve content from an up-stream box.
I've got this beautifully simple function to stream data from the up-stream to the client:
function(req, res){
request("http://example.com").pipe(res);
}
The upstream box is returning a cache header Cache-Control: no-cache
that I'd like to modify, so that Nginx (reverse proxy) can cache the response.
Where should I put the res.header('Cache-Control', 60);
?
I've tried:
function(req, res){
var retrieve = request("http://example.com");
retrieve.on('data', function(chunk){
if(res.get('Cache-Control') != 60)
res.header('Cache-Control', 60);
});
retrieve.pipe(res);
}
But this throws a Error: Can't set headers after they are sent
error.
Is there a listener that fires when the headers are sent, but before writeHeader()
is called?