Search code examples
node.jsexpressexpress-session

Difference between req.sessionID and req.session.id in express-session


Can someone explain me the difference between req.sessionID and req.session.id with an example? In a request shouldn't they be same always?


Solution

  • There is none. Use the source.

    Excerpt of the relevant constructor function:

    function Session(req, data) {
      Object.defineProperty(this, 'req', { value: req });
      Object.defineProperty(this, 'id', { value: req.sessionID });
    
      // ...
    }
    

    The reason why there are two ways to get the session ID is either "for convenience" or "for backward compatibility" – very probably both.