0

I have a strange feeling on using node server ..

Firstly I created a express http node server which i call as server1 that will render pages to the users ... and I created another express http node server which is server2 in my terms that will expose the services (business logic) that are actually invoked by server1.

When users are interacting with server1 in browser .. each user request is unique to the server1 with a unique session ID..

But the strange thing is when i request the server2 from server1 using the request npm module and make the REST calls to invoke a service in server2... Here server2 considers requests from server1 as a common one .... I mean even multiple users request the server1 that inturn requests the server2 .. the server2 treats all the requests as a single session... Server2 does not differentiate the requests from the different users ...

Code in Server2: 
   app.get('/service2' , function(req , res) {
       if(req.session.userID==null){
          req.session.userID == 1;
          console.log("Session is just created for the user");
       }else{
          console.log("session userid" + req.session.userID);
       }

   });

Here the very first request to the server2 from server1 will be a null session ... after that every user can print the console statement in the else block.

Code in Server1 : 

  app.get('/getuserreq' , function(req , res) {
       if(req.session.userID==null){
          req.session.userID == 1;
          console.log("Session is just created for the user");
       }else{
          console.log("session userid" + req.session.userID);
       }

   });

But the same code in server1 prints the "session is just created" message for the first request of each & every user from a browser window.

Could you pls tell me how to work on request identification if we get calls to server2 from server1 ...in a way to indentify the end user session ??????


Adding the additional info below :

middlewares in server1 are cookieParser,session (using sessionStore), json,methodOverride,router) .. same in server2 also ....

Server2 is invoked using request module

Request.post({
    uri:endpoint,
    method: 'POST',
    json:true,
    proxy: '', 
    headers: {'Content-Type':'application/x-www-form-urlencoded'},
    body: req.body
},function(err,extResponse,extResponseBody)
{
    var data = {};
    if(err)
    {
        log.error('Error in App layer : ',err);
        data.status = 2;
    }
    else
    {

        data.status = 1;
        data.emailid = extResponseBody.emailid;
        data.balance_records = extResponseBody.available_balance_records;
        data.cmrecords = extResponseBody.records;
        res.jsonp(data);
    }
});

visit the link - https://github.com/mikeal/request

user2749751
  • 405
  • 7
  • 22

1 Answers1

0

Request by default doesn't remember cookies. You have to set the jar option as defined in here: https://github.com/mikeal/request#examples

So you should change your code to something like this:

Request.post({
    uri:endpoint,
    method: 'POST',
    json:true,
    proxy: '', 
    headers: {'Content-Type':'application/x-www-form-urlencoded'},
    body: req.body,
    jar: true
}, ...
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
  • can not add jar:true for a post method. – user2749751 May 19 '14 at 10:54
  • Why not? It worked fine for me. And I just realized in the shown route handler you you specified `app.get('...` which should be `app.post('..` according to the request. – Farid Nouri Neshat May 19 '14 at 11:00
  • I just applied jar:true in the request Options parameter .. and got the error as TypeError: Object true has no method 'get' at Request.jar (C:\Users\babur\WebstormProjects\bvweb_19may14\node_modules\request\index.js:1149:19) without jar:true .. there is no error – user2749751 May 19 '14 at 11:57
  • Which version of request are you using? Try to update to the newest version if possible. – Farid Nouri Neshat May 19 '14 at 12:01
  • I updated request module and now able to work with jar. see my below : I am requesting from server1(running on port 3000) to server2(running on port 3001) var j = extRequest.jar(); var cookie = extRequest.cookie('\'name='+req.sessionID+'\''); j.setCookie(cookie,'http://localhost:3001/'); but i see this time ..different session ids at server2 for each request .. But i am not able to retrieve the set cookies in the request ... Do you tell me how to get those values of cookie in server2 ..? – user2749751 May 19 '14 at 15:13