Node.js服务器重新启动会话 [英] Node.js server restart drops the sessions
问题描述
app.use(express。 session({store:require('connect')。session.MemoryStore({reapInterval:60000 * 10})}))
唯一的问题是每次执行服务器重新启动时,会话都会丢弃。
https://github.com/remy/nodemon - 每次检测到文件更改时,nodemon重新启动node.js。
如何持续会话?
像你的代码告诉你正在使用MemoryStore。这是易失性的,并在重新启动时被清除。我建议您使用 connect_redis 来保留会话。 Redis是一个非常快速的商店。
- 下载 redis
- 编译redis:
make
- 启动服务器:
./ redis-server
-
npm install connect-redis
-
var connect = require('connect'),RedisStore = require连接-redis的);
connect.createServer(
connect.cookieParser(),
// 5分钟
connect.session({store:new RedisStore})
) ;
这只是为了让你快速入门。您应该阅读文档并配置redis,如果您想要充分利用redis。
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 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.
- Download redis
- compile redis:
make
- Start server:
./redis-server
npm install connect-redis
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.
这篇关于Node.js服务器重新启动会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!