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设置会话数据,我的终端/浏览器记录得很好。 如果我在浏览器网址栏中访问route1,则数据将成功显示在浏览器和终端中
如果我尝试在react组件中获取route2,我不会得到存储在req.session.test中的数据,并且我的终端仅记录其中的cookie部分,如下所示:
TCL: req.session Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: false,
secure: false } }
config:
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});
答案 0 :(得分:0)
尝试使用withCredentials: true
代替credentials: 'include'
我在官方文档中找不到凭证选项。
答案 1 :(得分:0)
我不是100%修复它的那部分,但是我安装了cors并以这种方式配置了我的抓取功能并解决了这个问题:
const isSession = await axios.get("http://localhost:5001/route1",
{credentials: 'include', proxy: true, withCredentials: true});
Index.js:
app.use(cors({
origin: ['*'],
methods: ['GET','POST'],
credentials: true // enable set cookie
}));
答案 2 :(得分:-1)
请确认。您必须按以下顺序配置Express:
app.use(express.cookieParser());
app.use(express.session({ secret: sessionVal }));
app.use(app.route);
It may solve your problem.