Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js server restart drops the sessions

Tags:

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 ?

like image 301
user80805 Avatar asked Mar 12 '11 22:03

user80805


People also ask

How do I restart a NodeJS server?

If it's just running (not a daemon) then just use Ctrl-C.

How session is maintained in NodeJS?

This cookie will contain the session's unique id stored on the server, which will now be stored on the client. This cookie will be sent on every request to the server. We use this session ID and look up the session saved in the database or the session store to maintain a one-to-one match between a session and a cookie.

How do I restart node server automatically?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.

What is req session destroy?

req. session. destroy // Deletes the session in the database.


2 Answers

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.

like image 133
Alfred Avatar answered Sep 27 '22 19:09

Alfred


I was trying to get Redis on track using express.js, Google sent me here. The express implementation changed:

var express = require('express'),
RedisStore = require('connect-redis')(express);

Another important thing is the order of express configurations.

app.configure(function(){

    app.enable('strict routing'); // removes trailing slash
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jqtpl');
    app.register('.html', require('jqtpl').express);
    app.use(express.favicon());
    app.use(express.methodOverride());
    app.use(express.compiler({src: __dirname + '/public', enable: ['sass']}));
    app.use(express.static(__dirname + '/public'));
    app.use(app.router);
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({secret: _.config.secret, store: new RedisStore}));

});

cookieParser & session configurations need to be at the end of the configurations, and cookieParser must be placed right before express.session.

Hope that helps, I ran in both of these problems.

like image 24
ezmilhouse Avatar answered Sep 27 '22 20:09

ezmilhouse