节点。js服务器重新启动会删除会话

时间:2021-07-25 22:46:04

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.

https://github.com/remy/nodemon -和nodemon重新启动节点。每次检测到文件更改时都是js。

How can I have persistent sessions ?

我怎么能坚持下去?

5 个解决方案

#1


23  

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.

就像你的代码告诉你在使用MemoryStore一样。这是不稳定的,在重新启动时清除。我建议您使用connect_redis来持久化会话。Redis是一家速度极快的商店。

  1. Download redis
  2. 下载复述,
  3. compile redis: make
  4. 编译复述:让
  5. Start server: ./redis-server
  6. 启动服务器:。/ redis-server
  7. npm install connect-redis
  8. npm安装connect-redis
  9.  

     

    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.

这只是为了让你尽快开始。您应该阅读文档并配置redis,如果您想从redis中获得大部分内容的话。

#2


14  

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

我试着用快递把Redis放在轨道上。js,谷歌派我来这里。表达实现改变:

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.

cookieParser & session配置需要放在配置的末尾,cookieParser必须放在express.session之前。

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

希望这对我有帮助,我遇到了这两个问题。

#3


8  

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.

我同意所有人对redis的看法,但我认为不同的技术在软件维护方面是一个问题。例如,如果使用mongodb,则有connect-mongo (https://npmjs.org/package/connect-mongo);如果使用mysql,则有connect-mysql (https://npmjs.org/package/connect-mysql);

#4


5  

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

此外,如果您正在使用express,您需要在告诉应用程序使用redis中间件时提供一个秘密。

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


1  

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

当node结束时,我可以想象您正在使用的内存存储将会终止。

Persist the sessions to disk?

将会话持久化到磁盘?

#1


23  

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.

就像你的代码告诉你在使用MemoryStore一样。这是不稳定的,在重新启动时清除。我建议您使用connect_redis来持久化会话。Redis是一家速度极快的商店。

  1. Download redis
  2. 下载复述,
  3. compile redis: make
  4. 编译复述:让
  5. Start server: ./redis-server
  6. 启动服务器:。/ redis-server
  7. npm install connect-redis
  8. npm安装connect-redis
  9.  

     

    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.

这只是为了让你尽快开始。您应该阅读文档并配置redis,如果您想从redis中获得大部分内容的话。

#2


14  

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

我试着用快递把Redis放在轨道上。js,谷歌派我来这里。表达实现改变:

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.

cookieParser & session配置需要放在配置的末尾,cookieParser必须放在express.session之前。

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

希望这对我有帮助,我遇到了这两个问题。

#3


8  

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.

我同意所有人对redis的看法,但我认为不同的技术在软件维护方面是一个问题。例如,如果使用mongodb,则有connect-mongo (https://npmjs.org/package/connect-mongo);如果使用mysql,则有connect-mysql (https://npmjs.org/package/connect-mysql);

#4


5  

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

此外,如果您正在使用express,您需要在告诉应用程序使用redis中间件时提供一个秘密。

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


1  

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

当node结束时,我可以想象您正在使用的内存存储将会终止。

Persist the sessions to disk?

将会话持久化到磁盘?