我试图让会话与节点一起工作。我使用Express 4.8.7。我正在学习here教程。
我在端口8080上运行的节点中构建了一个API。我在端口8081上运行了一个Angular应用程序。
在我的server.js文件的顶部,我有:
var session = require('express-session');
var bodyParser = require('body-parser');
app.use(session({
secret: 'ssshhhhh'
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var sess;
当我登录时,我会这样做:
app.post('/login', function(req, res) {
var successCallback = function(res, status, content) {
sess = req.session;
sess.email = req.body.email;
console.log('in LOGIN and req.session is ', req.session);
sendJsonResponse(res, status, content);
}
ctrlUsers.signin(req, res, successCallback);
});
登录后,前端会向/ files端点发出请求:
app.get('/files', function(req, res){
console.log('in files route and req.session is ', req.session);
});
在我上面链接的示例中,req.session应该包含一个带有电子邮件地址的属性。但它没有,它只包含:
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true } }
为什么会话未正确保存的任何想法?
答案 0 :(得分:-1)
更改
app.get('/files', function(req, res){
console.log('in files route and req.session is ', req.session);
});
到==>
var usersession = req.session;
app.get('/files', function(req, res){
console.log('in files route and req.session is ', usersession);
});