【发布时间】:2020-01-04 11:37:01
【问题描述】:
enter image description hereenter image description here如何在本地存储中设置令牌?现在它在响应正文中发送,我应该使用 res.setheader 吗?如果是,将向它插入什么令牌以便它将设置为本地存储?
const token = jwt.sign({
name: result[0].name
},
'somesupersecretsecret', {
expiresIn: '1h'
}
);
res.status(200).json({
token: token,
userId: "userid"
})
.catch(err => {
if (!err.statusCode) {
err.statusCode = 500;
}
next(err);
});
}
});
【问题讨论】:
-
正如您在格式化代码中看到的那样,括号存在问题。另外你需要知道服务端代码和客户端代码的区别
-
localStorage 可以在客户端使用。 Node.js 不附带 localStorage。也许你的意思是你想设置一个 cookie?
-
我添加了前端和后端的图像,问题是我应该如何将令牌发送到客户端以便将其存储在其浏览器上?应该像 res 这样的 cookie 发送.setheader 或者有一个选项可以将其存储为会话或本地存储?写入方式是什么
标签: javascript node.js jwt