简体   繁体   English

nodejs - Set-Cookie 出现在响应中但在浏览器中丢失

[英]nodejs - Set-Cookie present in response but missing in browser

I'm working with express.js and passport.js as my backened with axios and vue.js as my frontend.我正在使用 express.js 和 passport.js 作为我的后端,axios 和 vue.js 作为我的前端。

I can see my set-cookie but the cookie is not present in my browser.我可以看到我的 set-cookie,但我的浏览器中不存在 cookie。 Pictures linked below.图片链接如下。

Response headers Request headers响应标头请求标头

This cookie is used to authenticate my user using passport.js.此 cookie 用于使用 passport.js 对我的用户进行身份验证。

The following is the code for my express.js.以下是我的 express.js 的代码。

 var app = express(); app.use(cors({ origin: "http://localhost:8080", credentials: true })); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use(session({ secret: 'p@ssw0rd' })); app.use(passport.initialize()); app.use(passport.session());

The method below will be called through vue.js methods.下面的方法将通过 vue.js 方法调用。

 const url = "http://localhost:3000/"; let axiosConfig = { withCredentials: true, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'http://localhost:3000/', 'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE' } } class user { static login(username, password) { return new Promise(async(resolve, reject) => { try { const res = await axios.post( `${url}login`, { username, password }, axiosConfig ); resolve(res.data); } catch (err) { reject(err); } }); }

Backend is run at localhost:3000 and frontend is run at localhost:8080后端在 localhost:3000 运行,前端在 localhost:8080 运行

Inspecting cookies.检查 cookies。 No data presented for selected host未显示所选主机的数据

I found the answer to my question and the solution is really stupid, nothing code related.我找到了我的问题的答案,解决方案真的很愚蠢,没有任何代码相关。 Since I am working on expressjs and a friend works on vuejs, he added two of the same folder that contains the axios methods.由于我正在研究 expressjs,而一个朋友正在研究 vuejs,因此他添加了两个相同的文件夹,其中包含 axios 方法。 I changed the part which is not referenced.我更改了未引用的部分。

The code I posted in my question works.我在我的问题中发布的代码有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM