【问题标题】:Google App Engine connection.session() errorGoogle App Engine connection.session() 错误
【发布时间】:2017-11-26 22:35:54
【问题描述】:
我的 App Engine 日志显示了这一点。
警告:connection.session() MemoryStore 不是
专为生产环境设计,因为它会泄漏
内存,并且不会超过单个进程。
现在我的 nodejs 应用程序无法使用会话。我该如何解决这个问题?
【问题讨论】:
标签:
node.js
google-app-engine
express
google-cloud-platform
google-apis-explorer
【解决方案1】:
假设您指的是用户身份验证,请注意AppEngine Node documentation 和example 中的以下代码部分:
// 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')]
});
}
默认的MemoryStore 回退基本上只是用于开发目的;您应该为实际使用指定一个更永久/可扩展的会话存储。
【解决方案2】:
我认为您正在使用 PM2 或您的服务器正在运行多个线程,并具有默认会话存储机制。这不会通过单线程解决方案进行扩展,这是您通常在开发模式中使用的。
因此,为了保持会话,您需要将其存储在某个地方。例如,Redis。
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
})
}));
代码来源:个人项目