Google App Engine connection.session() 错误

Google App Engine connection.session() error

我的 App Engine 日志显示了这一点。

Warning: connection.session() MemoryStore is not

designed for a production environment, as it will leak

memory, and will not scale past a single process.

现在我的 nodejs 应用程序无法使用会话。我该如何解决?

我认为您正在使用 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
  })
}));

代码来源:个人项目

假设您指的是用户身份验证,请注意 AppEngine Node documentation and 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 回退基本上只是用于开发目的;您应该指定更多 permanent/scaleable 您选择的会话存储以供实际使用。

使用 会话存储 修复了错误。

使用这个 link。 https://github.com/expressjs/session#compatible-session-stores

README.mdcompatible session stores 有 expressjs 的兼容会话存储列表。