Search code examples
node.jsexpressnode.js-connect

Connect session middleware - regenerate vs. reload


I am trying to get a hang of Connect's Session middleware, and I would like to know the difference between: Session.regenerate() vs Session.reload().

Specifically, I checked the docs, and no explanation was given about what session reload actually does. Similarly, I am also confused about Session.save() method. Any help greatly appreciated!


Solution

  • Comparing the source code for the 2 functions:

    store.js

    Store.prototype.regenerate = function(req, fn){
      var self = this;
      this.destroy(req.sessionID, function(err){
        self.generate(req);
        fn(err);
      });
    };
    

    and

    session.js

    defineMethod(Session.prototype, 'reload', function reload(fn) {
      var req = this.req
        , store = this.req.sessionStore;
      store.get(this.id, function(err, sess){
        if (err) return fn(err);
        if (!sess) return fn(new Error('failed to load session'));
        store.createSession(req, sess);
        fn();
      });
      return this;
    });
    

    I read it as "get the session if it exists or create one" vs "destroy the previous and give me a new one".