Node.js server restart drops the sessions

后端 未结 5 1753
Happy的楠姐
Happy的楠姐 2020-12-24 03:40

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

app.use(express.session({store: require(\'connect\').ses         


        
相关标签:
5条回答
  • 2020-12-24 04:05

    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.

    0 讨论(0)
  • 2020-12-24 04:07

    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 }));
    
    0 讨论(0)
  • 2020-12-24 04:20

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

    Persist the sessions to disk?

    0 讨论(0)
  • 2020-12-24 04:25

    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.

    0 讨论(0)
  • 2020-12-24 04:30

    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.

    0 讨论(0)
提交回复
热议问题