将 cookie 添加到节点 JS 中的获取请求中的响应对象?



我正在尝试将cookie添加到不同get请求中的响应对象(res(。但是,如果我尝试以下操作,它会给我一个错误,即发送后我无法设置标头。我假设通过调用"请求"我已经发送了标头,如何使用来自单独 get 请求的数据将 cookie 添加到响应对象?同步性质也不会让我将数据保存在 get 请求之外。顺便说一句,我正在使用来自 npm 的请求模块,谢谢。

/* GET home page. */
router.get('/', function(req, res, next) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
var status = "Not currently logged in.";
if (req.cookies.token !== undefined) {
status = "Currently Logged in.";
}
if (req.cookies.email !== undefined) {
request('https://localhost:44338/api/customer/' + req.cookies.email 
{json: true}, (err, response, body) => {
res.cookie('user', body[0].customerID, {maxAge: 9000000});
//console.log(body);
});
}
res.render('index', { title: 'Mighty Morphin Store', data: "", status: status});
});

您必须在设置 cookie 后在回调函数中发送响应。

/* GET home page. */
router.get('/', function(req, res, next) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
var status = "Not currently logged in.";
if (req.cookies.token !== undefined) {
status = "Currently Logged in.";
}
if (req.cookies.email !== undefined) {
request('https://localhost:44338/api/customer/' + req.cookies.email 
{json: true}, (err, response, body) => {
res.cookie('user', body[0].customerID, {maxAge: 9000000});
//console.log(body);
res.render('index', { title: 'Mighty Morphin Store', data: "", status: status});
});
}
});

最新更新