Javascript 特快专场获胜';不要坚持并赢得胜利';不要向浏览器发送cookie
我正在开发一个mern stack(react、node、express和mongodb)web应用程序。 我已经在node.js上安装了express session。但是,我在浏览器中没有看到connect.sid cookie。此外,会话似乎不会在节点中的请求之间持久化 我最初认为这是cors的问题(可能仍然是这样),所以我尝试了一些对cors标题的调整,但没有任何运气Javascript 特快专场获胜';不要坚持并赢得胜利';不要向浏览器发送cookie,javascript,node.js,reactjs,express,express-session,Javascript,Node.js,Reactjs,Express,Express Session,我正在开发一个mern stack(react、node、express和mongodb)web应用程序。 我已经在node.js上安装了express session。但是,我在浏览器中没有看到connect.sid cookie。此外,会话似乎不会在节点中的请求之间持久化 我最初认为这是cors的问题(可能仍然是这样),所以我尝试了一些对cors标题的调整,但没有任何运气 //this is the main app.js file in node.js var session = req
//this is the main app.js file in node.js
var session = require('express-session')
app.use((req, res, next) => {
res.header('Access-control-Allow-Origin', '*');
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.header('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
预期结果是:express framework上的持久会话id和浏览器中存储的cookie文件
实际结果:会话不会持久化,并且不会存储cookie。修复更新:
问题是没有在fetchapi上设置credentials init选项
正确的代码应该这样编写:
// this is the React-Redux login action on the client side
export const fetchUser = (userData) => dispatch => {
fetch("http://localhost:3000/login", {
method: 'POST',
headers: { 'content-type': 'application/json' },
credentials: "include",
body: JSON.stringify(userData)
}).then(response => response.json())
.then(user =>
dispatch({
type: FETCH_USER,
payload: user
})
);
};
此外,在CORS设置中,通配符(“*”)不能用作“访问控制允许原点”。相反,它需要原始地址,在我的例子中是http://localhost:3001.
//this is the main app.js file in node.js
app.use((req, res, next) => {
res.header('Access-control-Allow-Origin', 'http://localhost:3001');
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.header('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
回想起来,我已经在一个相对早期的阶段就发现了这一点。但我不知道,在做出这些改变后,重置nodemon是不够的。
需要手动关闭服务器才能进行更改。修复更新:
问题是没有在fetchapi上设置credentials init选项
正确的代码应该这样编写:
// this is the React-Redux login action on the client side
export const fetchUser = (userData) => dispatch => {
fetch("http://localhost:3000/login", {
method: 'POST',
headers: { 'content-type': 'application/json' },
credentials: "include",
body: JSON.stringify(userData)
}).then(response => response.json())
.then(user =>
dispatch({
type: FETCH_USER,
payload: user
})
);
};
此外,在CORS设置中,通配符(“*”)不能用作“访问控制允许原点”。相反,它需要原始地址,在我的例子中是http://localhost:3001.
//this is the main app.js file in node.js
app.use((req, res, next) => {
res.header('Access-control-Allow-Origin', 'http://localhost:3001');
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.header('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
回想起来,我已经在一个相对早期的阶段就发现了这一点。但我不知道,在做出这些改变后,重置nodemon是不够的。
需要手动关闭服务器才能进行更改。更新:我仍然没有解决此问题,我只是注意到我正在使用postman而不是浏览器获取会话cookie。你有解决方案吗…我被困在这里了吗?更新:我仍然没有解决此问题,我刚刚注意到,我得到了一个与邮递员,但不是与浏览器会话cookie。你有解决方案…我被困在这里?