即使MongoStore使用connect-mongo,Express会话也不会持久

时间:2017-10-18 20:57:24

标签: node.js mongodb session express-session connect-mongo

当我重新启动服务器时,我的会话结束,我退出了。这不会发生在常规页面刷新上。我正在使用connect-mongo来解决这个问题:

var session = require('express-session'); const MongoStore = require('connect-mongo')(session);

以下是我用来存储会话的代码,重用现有的名为thisDb的Mongo连接:

app.use(session({                                
        secret: secretHash,
        saveUninitialized: true,
        resave: true,
        secure: false,
        store: new MongoStore({ db: thisDb })
}));

成功登录期间:

var day = 60000*60*24;
req.session.expires = new Date(Date.now() + (30*day));          
req.session.cookie.maxAge = (30*day);  

在我的Mongo shell中,我可以在登录时验证是否创建了新会话:

db.sessions.find()

{"cookie":{"originalMaxAge":2592000000,"expires":"2017-11-17T20:36:12.777Z","httpOnly":true,"path":"/"},"user":{"newNotifications":false,"username":"max","admin":"true","moderator":"true"},"expires":"2017-11-17T20:36:10.556Z"}

1 个答案:

答案 0 :(得分:0)

好吧,差不多三年后,我遇到了这个问题。不知道OP是否正在使用Passport,但我通过将此功能从password.use内部移动到外部来解决了此问题:

datadict = {}
datadict["array1"] = np.random.randint(255, size=(4, 4))
datadict["array2"] = np.random.randint(255, size=(4, 4))
datadict["array3"] = np.random.randint(255, size=(4, 4))
datadict["array4"] = np.random.randint(255, size=(4, 4))

# Generate a dict with variable name key and median value
d = {k:np.median(v) for k,v in datadict.items()}