// In production use the App Engine Memcache instance to store session data,
// otherwise fallback to the default MemoryStore in development.
if (config.get('NODE_ENV') === 'production' && config.get('MEMCACHE_URL')) {
sessionConfig.store = new MemcachedStore({
hosts: [config.get('MEMCACHE_URL')]
});
}
const EXPRESS = require('express');
const APP = EXPRESS();
const EXPRESS_SESSION = require('express-session');
const REDIS_STORE = require('connect-redis')(EXPRESS_SESSION);
APP.use(EXPRESS_SESSION({
secret: 'YOUR_SECRET',
saveUninitialized: false,
resave: false,
store: new REDIS_STORE({ //storing the session in redis
host: 'localhost',
port: 6379, //redis port, should be 6379 by default
ttl: 300 //time-to-live, session will be destroyed if no activity in 5 mins
})
}));
3 回答
假设您指的是用户身份验证,请注意AppEngine Node documentation和example中代码的以下部分:
默认的
MemoryStore
回退基本上只是用于开发目的;您应该为实际使用指定一个更永久/可扩展的会话存储 .我认为您正在使用PM2或您的服务器正在运行多个线程,具有默认会话存储机制 . 这不会扩展传递单线程解决方案,这是您在开发模式中通常使用的解决方案 .
因此,为了保持会话,你需要将它存储在某个地方 . 例如,Redis .
代码来源:个人项目
使用 session store 修复了错误 .
使用此链接 . https://github.com/expressjs/session#compatible-session-stores
在 README.md 下 compatible session stores ,有快递的兼容会话存储列表 .