【问题标题】:Set cookie for domain instead of subDomain using NodeJS and ExpressJS使用 NodeJS 和 ExpressJS 为域而不是子域设置 cookie
【发布时间】:2013-08-17 16:55:47
【问题描述】:

我一直在使用 expressjs 和 mongostore 进行会话管理。下面是expressjs中配置store的代码,

app.configure(function(){
    app.use(express.session({
        secret: conf.secret,
        maxAge: new Date(Date.now() + 3600000),
        cookie: { path: '/' },
        store: new MongoStore(conf.db)
    }));
});

我在上面的代码中提到了 cookie 路径。但它将 cookie 设置在 sub.domain.com 而不是 .domain.com 中。我如何做到这一点?

【问题讨论】:

标签: cookies node.js express session-cookies


【解决方案1】:

像这样配置它:

app.use(express.session({
    secret: conf.secret,
    cookie: { domain:'.yourdomain.com'},
    store: new MongoStore(conf.sessiondb)
}));

【讨论】:

  • 是否可以为多个域设置 cookie?例如:cookie:{域:'.yourdomain1.com','yourdomain2.com},
  • 我刚试过example.com,它奏效了。在那里添加. 有什么区别吗?
【解决方案2】:

尝试使用以下链接进行配置。

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

链接: http://expressjs.com/api.html#res.cookie

【讨论】:

  • 为什么当我只添加这部分“域:'.example.com'”时,cookie没有添加,当我删除它时,cookie被添加?
  • 我刚试过example.com,它奏效了。在那里添加. 有什么区别吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
相关资源
最近更新 更多