I've got a proxy set up in nodejs that goes to one of our backend servers for data; some of that data (such as session id) is stored as cookies. what I want to do is have the proxy get the remote cookies, push then into the header of the response to the original request, then send the response back. I'm close, but hit a snag:
I've got a proxy set up in nodejs that goes to one of our backend servers for data; some of that data (such as session id) is stored as cookies. what I want to do is have the proxy get the remote cookies, push then into the header of the response to the original request, then send the response back. I'm close, but hit a snag:
app.get(/\/json\/(.+)/, getJson);
var getJson = function(req, res) {
console.log("going to " + config.scraperUrl.replace('http://', ''));
res.setHeader('Content-Type', 'application/json; charset=utf-8');
utils.setCookies(res, ["floo=flum"]) // this works okay
var options = {
host : config.scraperUrl.replace('http://', ''),
path : '/rwd/' + req.params[0] + '?' + querystring.stringify(req.query),
method : "GET",
rejectUnauthorized : false
};
var request = https.request(options, function(response) {
console.log("statusCode: ", response.statusCode);
response.setEncoding('utf8');
console.log("headers: "+response.headers['set-cookie']);
utils.setCookies(res, ["flib=flah"]) // this doesn't work
response.on('data', function(d) {
res.write(d)
});
response.on('end', function() {
res.end()
});
});
request.end();
request.on('error', function(e) {
console.error("error occurred: " + e.message);
res.end();
});
}
setCookies(response1, cookies) just loops thru the cookies and does res.setHeader('Set-Cookie', cookie)
The problem is that it looks like the headers have been baked by the time the second setCookies is called; moving the method to the 'data' event handler does not help. The error I get is:
http.js:689
throw new Error('Can\'t set headers after they are sent.');
Any way to add headers to response1 that I receive from the response2?
var getJson = function(req, res) {
var options = {
host : config.scraperUrl.replace('http://', ''),
path : '/rwd/' + req.params[0] + '?' + querystring.stringify(req.query),
method : "GET",
rejectUnauthorized : false
};
var firstResponse = true;
var request = https.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function(d) {
if (firstResponse) {
var cookies = response.headers['set-cookie'];
res.setHeader('Content-Type', 'application/json; charset=utf-8'); // write to header-- this fails
utils.setCookies(res, ["floo=flum"]) // never get here
firstResponse = false;
}
res.write(d)
});
response.on('end', function() {
res.end()
});
});
request.end();
request.on('error', function(e) {
console.error("error occurred: " + e.message);
res.end();
});
}
/***** json requests to php side *******/
app.get(/\/json\/(.+)/, getJson);