NodeJS 如何在一个子域中设置cookie并从同一域中的另一个子域访问它?

vktxenjb  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(107)

我正在使用一个子域在服务器上部署后端,并在另一个子域上部署前端。但是,没有为前端设置cookie。我已经尝试使用domain: '.domain.com'选项来设置cookie,但它不起作用。有谁能帮我弄清楚这个问题可能是什么?
我尝试了解决方案,但cookie仍然没有设置:
res.cookie('myCookie', 'myValue', { domain: '.example.com' });
res.cookie('myCookie', 'myValue', { domain: '.example.com', path: '/', secure: true });

enxuqcxy

enxuqcxy1#

这可能会回答您的问题https://stackoverflow.com/a/23086139/22070854,但我认为您忘记指定cookie的到期日期。
假设你使用express,你可以这样做:

res.cookie('cookieName', 'cookieValue', {

  // the domain name
  domain: '.domain.com',

  // the expiry time relative to the current time (in milliseconds), 1 hour here
  maxAge: 1000 * 60 * 60,

  // the path
  path: '/'
});

相关问题