Node.js服务器重新启动会话 [英] Node.js server restart drops the sessions

查看:150
本文介绍了Node.js服务器重新启动会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  app.use(express。 session({store:require('connect')。session.MemoryStore({reapInterval:60000 * 10})}))

唯一的问题是每次执行服务器重新启动时,会话都会丢弃。



https://github.com/remy/nodemon - 每次检测到文件更改时,nodemon重新启动node.js。



如何持续会话?

解决方案

像你的代码告诉你正在使用MemoryStore。这是易失性的,并在重新启动时被清除。我建议您使用 connect_redis 来保留会话。 Redis是一个非常快速的商店。


  1. 下载 redis

  2. 编译redis: make

  3. 启动服务器: ./ redis-server

  4. npm install connect-redis

  5.   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.

  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.

这篇关于Node.js服务器重新启动会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆