Transferring cookies in node-js

920 views
Skip to first unread message

Jeff Lowery

unread,
Aug 7, 2014, 5:53:26 PM8/7/14
to expre...@googlegroups.com

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?



greelgorke

unread,
Aug 8, 2014, 2:58:05 AM8/8/14
to expre...@googlegroups.com
just postprone your first setting any headers till you got response from upstream. because your request is async, node tries to get best performance and writes the headers to the out buffer asap. also you should set your cookies in a single call.

Jeff Lowery

unread,
Aug 8, 2014, 2:47:35 PM8/8/14
to expre...@googlegroups.com
That makes sense, but I'm either not understanding correctly or the header is baked regardless if it's been written too:








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);



Jeff Lowery

unread,
Aug 8, 2014, 2:51:55 PM8/8/14
to expre...@googlegroups.com
I think a better approach might be to use express session module to store the second response's cookies in the first response's session (stored on node-js); subsequent calls to the backend server would push cookie values from the session to the request header, and modify/add cookies in the session based on the response.  The browser would remain cookieless, except for the node-js session id cookie.  

greelgorke

unread,
Aug 8, 2014, 2:56:45 PM8/8/14
to expre...@googlegroups.com
i'm not sure why it fails, but here some remarks:

you shouldn't handle cookies in the data-handler. as soon as your request callback is called you have the cookies, you don't need firstResponse variable at all.

please make sure that there are no other places setting headers or sending something. including middleware.
Reply all
Reply to author
Forward
0 new messages