最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - res.setHeader("Set-Cookie", ...) is not setting the cookie in NodeExpress - Stack Overflow

programmeradmin2浏览0评论

I have tried

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2", { path: "/", domain: "localhost" })

and

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2"))

and I have tried to set the cookie outright with

res.setHeader(
    "Set-Cookie",
    "name=name"})

but no cookie is set when I examine the res, and no cookie is received in my frontend. Everything is run locally. I do not understand why.

I have tried

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2", { path: "/", domain: "localhost" })

and

  res.setHeader(
    "Set-Cookie",
    cookie.serialize("name", "name2"))

and I have tried to set the cookie outright with

res.setHeader(
    "Set-Cookie",
    "name=name"})

but no cookie is set when I examine the res, and no cookie is received in my frontend. Everything is run locally. I do not understand why.

Share Improve this question asked Jun 14, 2019 at 17:13 ClownBabyClownBaby 4781 gold badge6 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I found the answer. When using fetch() from the front end I didn't set the credentials flag to include. When I included the flag it worked instantly.

  return await fetch(
    `url`,
    { credentials: "include" }

If you just want to make a cookie then try this instead of res.setHeader():

res.cookie('cookieName', 'cookieValue');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论