I'm looking at this answer on how to proxy a request through nodejs. Here's the code presented:
function onRequest(client_req, client_res) {
console.log('serve: ' + client_req.url);
var options = { ... };
var proxy = http.request(options, function (res) {
client_res.writeHead(res.statusCode, res.headers) <--------- why this?
res.pipe(client_res, {
end: true
});
});
client_req.pipe(proxy, {
end: true
});
}
Can anyone explain why I need to writeHead
manually? My assumption is that headers will be piped from the response of the server the request is proxied to?