Node.js server restart drops the sessions

user80805 picture user80805 · Mar 12, 2011 · Viewed 10.8k times · Source

I'm having fully functional user signup/authentication system using express and connect middleware.

app.use(express.session({store: require('connect').session.MemoryStore( {reapInterval: 60000 * 10} ) }))

The only problem is that sessions drop every time you perform server restart.

https://github.com/remy/nodemon - and nodemon restarts node.js every time it detects a file change.

How can I have persistent sessions ?

Answer

Alfred picture Alfred · Mar 13, 2011

Like your code is telling you are using MemoryStore. This is volatile and gets cleared on restart. I would advise you to use connect_redis to persist your session. Redis is an extremely fast store.

  1. Download redis
  2. compile redis: make
  3. Start server: ./redis-server
  4. npm install connect-redis
  5.  

    var connect = require('connect') , RedisStore = require('connect-redis');
    
    connect.createServer(
      connect.cookieParser(),
      // 5 minutes
      connect.session({ store: new RedisStore })
    );
    

This is just to get you started quickly. You should read the documentation and configure redis if you want to get most out of redis.