Background:
My server is using Node.js, Express, and includes tools such as CORS, express-session, and connect-mongo. It utilizes HTTP requests. However, the browser is unable to retrieve cookies from the Set-Cookie response in the interface.
I am currently learning to use Node.js, and I want to implement cookie + session login session control. However, I have been troubled by the browser cookie issue for a long time. Thank you very much for your answer.
Additionally: My English is not good.
相关背景:
我的服务器使用了nodejs, express, 并且使用了工具: cors, express-session, connect-mongo, 使用http请求, 但是我在接口响应的Set-Cookie, 浏览器无法获取
我正在学习使用nodejs, 我想实现一个cookie + session的登录会话控制, 但是我被浏览器cookie这个问题困扰很久, 感谢您解答
Image One: My Node.js interface has already returned the cookie.
图片1: 我的接口返回了cookie了
enter image description here
Image Two: My browser cannot obtain the cookie, and document.cookie also cannot retrieve it.
图片2: 但是我浏览器无法获取cookie
=============================================
The solution I tried:
-
Cookie settings: httpOnly: false, sameSite: ‘None’.
enter image description here -
I attempted to obtain an HTTPS certificate using SSL, but I am using a local server with 127.0.0.1. I tried to apply for HTTPS, but without success.
(我本地服务器, 尝试获取https证书不成功) -
I set
withCredentials: true
, but encountered an error. I attempted to use the request headerAccess-Control-Allow-Origin: http://127.0.0.1
, but it still didn’t work.
(设置withCredentials: true
, 但是报错, 我尝试使用请求头Access-Control-Allow-Origin: http://127.0.0.1
, 但还是不行)
enter image description here
=============================================
The above are some of the solutions I tried, but I still can’t retrieve cookies in the browser.
Additionally, my browser is running at 127.0.0.1:80, while the Node server is at 127.0.0.1:3000.
(以上是我采取的部分解决办法, 但我还是无法在浏览器中获取cookie,
再补充一点, 我的浏览器是127.0.0.1:80, node服务器是127.0.0.1:3000, )
Thank you once again.