Node.js客户端会话不创建req.session [英] Node.js client-sessions not creating req.session
问题描述
我遇到节点客户端会话问题,所以我尝试使用这个示例应用程序从 https://github.com/fmarier/node-client-sessions-sample 。然而,即使使用这个基本的应用程序,我得到 TypeError:无法读取未定义的属性'username'
- req.session
未定义。以下是完整的代码:
I was having issues with node-client-sessions, so I tried using this sample application from https://github.com/fmarier/node-client-sessions-sample. However, even with this basic app I get TypeError: Cannot read property 'username' of undefined
- req.session
is undefined. Here's the code in full:
#!/usr/bin/env node
const
app = require('express')(),
clientSessions = require("client-sessions");
app.use(clientSessions({
secret: '0GBlJZ9EKBt2Zbi2flRPvztczCewBxXK' // CHANGE THIS!
}));
app.get('/', function (req, res){
if (req.session.username) {
res.send('Welcome ' + req.session.username + '! (<a href="/logout">logout</a>)');
} else {
res.send('You need to <a href="/login">login</a>.');
}
});
app.get('/login', function (req, res){
req.session.username = 'JohnDoe';
console.log(req.session.username + ' logged in.');
res.redirect('/');
});
app.get('/logout', function (req, res) {
req.session.reset();
res.redirect('/');
});
app.listen(3000);
package.json
has:
"dependencies": {
"express": ">= 3",
"client-sessions": ">= 0.0.9"
}
我是否去localhost:3000或locahost:3000 / login我得到 TypeError:无法读取未定义的属性'username'
Whether I go to localhost:3000 or locahost:3000/login I get TypeError: Cannot read property 'username' of undefined
发生了什么?节点客户端会话不应该创建 req.session
?
What's going on? Shouldn't node-client-sessions be creating req.session
?
推荐答案
p>是的,事实证明,只是 req
属性的默认名称从会话
更改为 session_state
。修改它。
Yeah as it turns out it's just that the default name of the req
property changed from session
to session_state
. Changing that fixed it.
这篇关于Node.js客户端会话不创建req.session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!