node.js 使用 express / redis 进行会话存储时未定义“会话”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10191692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'session' is undefined when using express / redis for session store
提问by Alex
I'm trying to use redis for sessions in my express app.
我正在尝试在我的 Express 应用程序中使用 redis 进行会话。
I do the following:
我执行以下操作:
var express = require('express');
var RedisStore = require('connect-redis')(express);
app.configure('development', function(){
app.use(express.session({ secret: "password",
store: new RedisStore({
host: "127.0.0.1",
port: "6379",
db: "mydb"
})
}));
Later on, in my app, if i do something like:
稍后,在我的应用程序中,如果我执行以下操作:
var whatever = req.session.someProperty;
I get:
我得到:
Cannot read property 'someProperty' of undefined
无法读取未定义的属性“someProperty”
This indicates that req.session is undefined (I can see this from a console.log entry in my config section)
这表明 req.session 未定义 (我可以从配置部分的 console.log 条目中看到这一点)
I've definitely got redis running, and can see my app connects to it initially (using redis-cli monitor)
我肯定已经运行了 redis,并且可以看到我的应用程序最初连接到它(使用 redis-cli 监视器)
采纳答案by JohnnyHK
回答by Yxven
Sessions won't work unless you have these 3 in this order:
除非您按以下顺序拥有这 3 个,否则会话将无法工作:
app.use(express.cookieParser());
app.use(express.session());
app.use(app.router);
I'm not sure if router is mandatory to use sessions, but it breaks them if it's placed before them.
我不确定路由器是否必须使用会话,但如果它放在它们之前会破坏它们。
回答by smilly92
Had the same problem, however it was caused by changes in the latest version of express.
有同样的问题,但它是由最新版本的 express 更改引起的。
You now need to pass express-session to the function connect-redis exports to extend session.Store:
您现在需要将 express-session 传递给函数 connect-redis 导出以扩展 session.Store:
var express = require('express');
var session = require('express-session')
var RedisStore = require('connect-redis')(session);
回答by sebilasse
Things have changed recently with Express 3 / Express 4. Please confirm you are using version 4.
Express 3 / Express 4 最近发生了变化。请确认您使用的是版本4。
The complete middleware concept changed. You need to install these middlewares manually. "express-session" is one of the 4.0 middlewares.
完整的中间件概念发生了变化。您需要手动安装这些中间件。“express-session”是 4.0 中间件之一。
I recommend to read
我建议阅读
http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0and https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x
http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0和https://github.com/visionmedia/express/wiki/Migrating-from- 3.x 到 4.x
Additionally some users were confused that the github repoitself is named just "session" but
此外,一些用户对github 存储库本身仅命名为“会话”感到困惑,但
npm install express-session
is correct.
是正确的。
回答by kaore
I had the same problem. It turned out that redis was simply configured to a different port.
我有同样的问题。原来,redis 只是简单地配置到了不同的端口。

