简体   繁体   English

在 koa 服务器中检查日志时,不会从浏览器(本地主机)发送 cookie

[英]cookie is not sent from browser (localhost) when check logs in koa server

I use Koa for api server, React in frontend, and use Axios for xhr.我将 Koa 用于 api 服务器,在前端使用 React,并将 Axios 用于 xhr。

when client request to api server in server-side-rendering, which means in node.js, I can see all headers coming from node.js.当客户端在服务器端渲染中向 api 服务器请求时,这意味着在 node.js 中,我可以看到所有来自 node.js 的标头。 when I test with PostMan, I see all the headers included in request also.当我使用 PostMan 进行测试时,我也看到了请求中包含的所有标头。 However, when client get into CSR and do axios request, I couldn't see any headers from logs in api server except但是,当客户端进入 CSR 并执行 axios 请求时,我在 api 服务器中的日志中看不到任何标头,除了

{
  accept: 'application/json',
  'content-type': 'application/json',
  'user-agent': 'axios/0.19.2',
  host: 'localhost:5001',
  connection: 'close'
}

I could find cookies are included in request headers when check chrome inspector - network tab, but couldn't find why my localhost browser can't send headers with cookies appropriate.在检查 chrome 检查器 - 网络选项卡时,我可以找到 cookies 包含在请求标头中,但找不到为什么我的本地主机浏览器无法发送带有 cookies 的标头。

for those who want to know how I setup both frontend and backend specifically,对于那些想知道我如何专门设置前端和后端的人,

Koa考阿

app.use(cors({
  origin: verifyOrigin, // http://localhost:5000
  credentials: true,
}));

...

cookies.set("someCookieKey", someCookieValue, {
      httpOnly: true,
      maxAge: 1000 * 60 * 60 * 24 * 7,
    });

in React在反应

const http = axios.create({
  baseURL,
  headers: {
    "Accept": "application/json",
    "Content-Type": "application/json",
  }
})

http.defaults.withCredentials = true;


After stumble upon on this for a while, I found I send request to server with out request object.在偶然发现了一段时间后,我发现我向服务器发送请求而没有请求 object。

If api server deal with some business logic with cookies or header something, you better check client side omitted cookie or not.如果 api 服务器使用 cookies 或 header 处理某些业务逻辑,则最好检查客户端是否省略了 cookie。

Probably httpOnly: true is a problem in this case.在这种情况下,可能httpOnly: true是一个问题。 That parameter blocks read cookie by js in browser.该参数会阻止浏览器中的 js 读取 cookie。 Try set value to false .尝试将值设置为false

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

相关问题 Koa Server-sent Events仅向浏览器中的EventSource产生错误 - Koa Server-sent Events yields only errors to EventSource in browser Cookie 发送到 localhost 但未发送到 https 域 - Cookie sent to localhost but not to https domain 为什么浏览器没有设置从我的 node.js 后端发送的 cookie? - Why browser is not setting the cookie sent from my node js backend? 浏览器发送了两个请求,但只有本地主机被调用 - Two request being sent from browser but only localhost is called 自定义 cookie 发送到浏览器但未存储 - Custom cookie sent to browser but not stored Google Chrome未设置从NodeJS / Express服务器发送的Cookie - Google Chrome not setting cookie sent from NodeJS/Express server 无法读取从 Express 服务器上的 React js 前端发送的 cookie - Unable to read cookie sent from React js frontend on Express server 不确定为什么在使用 express-session 时没有 cookie 被发送回我的本地主机客户端 - Unsure as to why no cookie is being sent back to my localhost client when using express-session 服务器发送的cookie不会覆盖现有的cookie - server sent cookie is not overriding the existing cookie 为什么cookie没有被发送到服务器 - Why is cookie not getting sent to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM