Node.js server restart drops the sessions

帅比萌擦擦* 提交于 2019-12-29 14:20:21

问题


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 ?


回答1:


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.




回答2:


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.




回答3:


I agree with everybody about redis, but I think that different technologies is a problem in terms of software maintenance. If you are using mongodb for example there is connect-mongo (https://npmjs.org/package/connect-mongo), if you are using mysql there is connect-mysql (https://npmjs.org/package/connect-mysql), connect-couchdb for couchdb (https://npmjs.org/package/connect-couchdb) and so on.




回答4:


also, if you're using express, you need to provide a secret when telling the app to use the redis middleware.

so, follow Alfred's recipe above, but do the following...

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

app.use( express.cookieParser() );
app.use( express.session( { secret: "keyboard cat", store: new RedisStore }));



回答5:


When node dies I would imagine the memory store you're using dies.

Persist the sessions to disk?



来源:https://stackoverflow.com/questions/5286073/node-js-server-restart-drops-the-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!