javascript - 我可以获取“req.session”(快速会话)数据吗?
问题描述
app.get("/route1", async (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
console.log('TCL: req.session', req.session);
res.send(req.session);
});
app.get("/route2", (req, res) => {
req.session.test = "test";
console.log('TCL: req.session', req.session);
res.send(req.session);
});
route2 设置会话数据,我的终端/浏览器记录它就好了。如果我在浏览器 url 栏中访问 route1,则数据会成功显示在浏览器和终端中
如果我尝试在 react 组件中获取 route2,我不会获得存储在 req.session.test 中的数据,并且我的终端只记录其中的 cookie 部分,如下所示:
TCL: req.session Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: false,
secure: false } }
配置:
app.use(session({
secret: prodkeys.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: false,
secure: false,
}
}));
在组件中:
const isSession = await axios.get("http://localhost:5001/route1",
{credentials: 'include', proxy: true});
解决方案
我不是 100% 修复了哪个部分,但我安装了 cors 并以这种方式配置了我的 fetch 并解决了它:
const isSession = await axios.get("http://localhost:5001/route1",
{credentials: 'include', proxy: true, withCredentials: true});
索引.js:
app.use(cors({
origin: ['*'],
methods: ['GET','POST'],
credentials: true // enable set cookie
}));
推荐阅读
- javascript - Electron.js 中的命令行保存文件错误
- discord - Discord.py 使 discord bot 对带有多个表情的消息做出反应
- javascript - 使用 Javascript 准确减去 100 分钟
- json - 如何评估字符串以识别重复键
- sql - 获取Oracle SQL没有记录的天数
- java - @ConditionalOnProperty Bean 配置未按预期工作
- powershell - Windows Defender 和处理下载的 PowerShell 脚本
- javascript - 通过单击播放图像(Jquery)从父 div 播放最后一个视频后发生错误
- python - 绘制密度分布图
- assembly - 在汇编 x86_64 中查找数组总和