简体   繁体   English

节点express-session中的会话变量不会持续到下一个请求

[英]Session variables in node express-session don't persist to the next request

I am using a basic node express-session setup with memory store and I have this code on the server: 我正在使用带有内存存储的基本节点快速会话设置,并且服务器上有以下代码:

app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}));

app.post('/api/logIn', function(req, res) {
    req.session.userName = req.body.userName;
}

app.get('/api/getProfile', function(req, res) { 
    User.findOne({'userName' : req.session.userName}, 
        function (err, userProfile) {
        console.log('getProfile executed for user:' + req.session.userName);
        if (err) throw err;
        console.log(userProfile);
    });
});

The problem is that req.session.userName from getProfile route is undefined, although it is not in the previous request to logIn route. 问题是,尽管在上一个登录路由请求中也没有定义getProfile路由中的req.session.userName ,但该定义未定义。 I inspected HTTP headers and strangely there are no headers dealing with cookies, from the server or from the client. 我检查了HTTP标头,奇怪的是,没有标头处理来自服务器或客户端的cookie。 Right now I have no idea what could be the problem. 现在我不知道可能是什么问题。

You say cookie: { secure: true } , but is your web server actually on a secure connection? 您说cookie: { secure: true } ,但是您的Web服务器实际上处于安全连接吗? If not, then the cookie won't be written. 如果不是,则将不会写入cookie。

From the docs : 文档

Please note that secure: true is a recommended option. 请注意,安全:true是推荐的选项。 However, it requires an https-enabled website, ie, HTTPS is necessary for secure cookies. 但是,它需要启用HTTPS的网站,即HTTPS对于安全cookie是必需的。 If secure is set, and you access your site over HTTP, the cookie will not be set. 如果设置了安全性,并且您通过HTTP访问站点,则不会设置cookie。

Also its important to note if you are using fetch() to make your API calls to include { credentials: 'include' } in the options of your fetch() call. 同样重要的是要注意,如果您正在使用fetch()进行API调用以在您的fetch()调用选项中包含{ credentials: 'include' } Otherwise the cookie will not set properly and your session will not persist. 否则,cookie将无法正确设置,并且您的会话将不会持续。 Make sure that on your server side you do something like: 确保在服务器端执行以下操作:

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

so that your headers are set properly and cors wont be an issue. 这样就可以正确设置标题,并且不会出现问题。 Took me awhile to figure this out but its working now! 花了我一段时间来解决这个问题,但现在可以正常工作!

The session need is stored in a cookie, so we use this to parse it, some like this: 会话需求存储在cookie中,因此我们使用它来解析它,如下所示:

var cookieParser = require('cookie-parser');

// must use cookieParser before expressSession
app.use(cookieParser());

Full example: http://code.runnable.com/U0tEnHwraXYyp-JG/simple-usage-of-express-session-and-cookie-parser-with-express-for-node-js 完整示例: http : //code.runnable.com/U0tEnHwraXYyp-JG/simple-usage-of-express-session-and-cookie-parser-with-express-for-node-js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM