使用 NodeJS 和 ExpressJS 为域而不是子域设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7834228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:41:37  来源:igfitidea点击:

Set cookie for domain instead of subDomain using NodeJS and ExpressJS

cookiesnode.jsexpresssession-cookies

提问by Raja

I have been using expressjs and mongostore for session management. Following is the code to configure store in expressjs,

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

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

I had mentioned the cookie path in the above code. But it sets the cookie in sub.domain.com instead of .domain.com. How do i achieve this?

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

回答by metis

configure it like this:

像这样配置它:

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

回答by AmirtharajCVijay

Try to use the following link to configure.

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

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

Link :http://expressjs.com/api.html#res.cookie

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