销毁 cookie NodeJs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27978868/
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
Destroy cookie NodeJs
提问by Manwal
I am using Cookiesmodule for setting cookie. Here is following my code:
我正在使用Cookies模块来设置 cookie。以下是我的代码:
var options = {
maxAge: ALMOST_ONE_HOUR_MS,
domain: '.test.com',
expires: new Date(Date.now() + ALMOST_ONE_HOUR_MS)
};
var value = userInfo.token;
cookies.set("testtoken", value, options);
But in documentation I haven't found how to destroythis cookie.
但是在文档中我还没有找到如何销毁这个 cookie。
Any suggestion would be appreciated.
任何建议将不胜感激。
采纳答案by Hozuki
There is no way to delete a cookie according to the HTTP specification. To effectively "delete" a cookie, you set the expiration date to some date in the past. Essentially, this would result in the following for you (according to the cookies module documentation):
根据 HTTP 规范,无法删除 cookie。为了有效地“删除”cookie,您可以将到期日期设置为过去的某个日期。本质上,这将为您带来以下结果(根据 cookie 模块文档):
cookies.set('testtoken', {maxAge: 0});
Or according to the HTTP specification:
或者根据 HTTP 规范:
cookies.set('testtoken', {expires: Date.now()});
Both of which should work. You can replace Date.now()with new Date(0)for a really old date.
两者都应该有效。您可以替换Date.now()与new Date(0)一个真正的老日期。
回答by vashishatashu
For webapp you can just set cookie in response as :
对于 webapp,您可以将 cookie 作为响应设置为:
res.cookie("key", value);
and to delete cookie :
并删除cookie:
res.clearCookie("key");
回答by Jesper Bylund
While one other answer is correct, deleting a cookie from an express.js webapp is done by invocing the following method:
虽然另一个答案是正确的,但通过调用以下方法从 express.js webapp 中删除 cookie:
res.clearCookie("key");
res.clearCookie("key");
But there's a caveat!
但有一个警告!
Your cookie options (except expires) need to be the same as when you set it. Otherwise browsers will NOT remove the cookie. So use the same domain, security setting etc. (reference: https://expressjs.com/en/4x/api.html#res.clearCookie)
您的 cookie 选项(过期除外)需要与您设置时相同。否则浏览器将不会删除 cookie。所以使用相同的域,安全设置等(参考:https: //expressjs.com/en/4x/api.html#res.clearCookie)
回答by Sandro Wiggers
I'm using this with cookie-parsermodule:
我在cookie-parser模块中使用它:
router.get('/logout', function(req, res){
cookie = req.cookies;
for (var prop in cookie) {
if (!cookie.hasOwnProperty(prop)) {
continue;
}
res.cookie(prop, '', {expires: new Date(0)});
}
res.redirect('/');
});
回答by Kavitha Vikas
To delete any http cookie if we just try to clear it from response [using res.clearCookie("key")], it is definitely not going to work. In reality, to delete http cookie, domain and path are very important.
要删除任何 http cookie,如果我们只是尝试从响应中清除它 [使用res.clearCookie("key")],它肯定不会起作用。实际上,要删除http cookie,域和路径非常重要。
Domain and path define the scope of the cookie. In face, they essentially tell the browser what website the cookie belongs to. Sending the same cookie value with ; expires appended is also a bad idea since you want the content to be destroyed, but that is not going to happen.
域和路径定义了 cookie 的范围。实际上,它们本质上是告诉浏览器 cookie 属于哪个网站。发送相同的 cookie 值;expires appended 也是一个坏主意,因为您希望内容被销毁,但这不会发生。
The best idea would be invalidating the cookie by setting the value to empty and include an expires field as well like below:
最好的办法是通过将值设置为空并包含一个 expires 字段来使 cookie 无效,如下所示:
res.cookie("key","empty the key content", {expires:old date, domain:'.example.com', path:'/'});
res.cookie("token", "", { expires: new Date(0),domain:'.test.com', path: '/' });
Hope this helps!!!
希望这可以帮助!!!
回答by Mark Colling
I was going through the same problem a few days ago. After discussing it with a friend, I think this is the best solution.
几天前我遇到了同样的问题。和朋友讨论后,我认为这是最好的解决方案。
res.setHeader('set-cookie', 'mycookie=; max-age=0');
Advantages:
好处:
- only use node
- simple to understand
- 只使用节点
- 简单易懂
credits: @andy
学分:@andy
回答by Ga?per Gra?ner
I am using cookie-parseras well, and upper answers lead me to the solution. In my case I needed to add overwrite: trueas well, otherwise new cookie key was added.
我cookie-parser也在使用,上面的答案引导我找到解决方案。在我的情况下,我也需要添加overwrite: true,否则添加了新的 cookie 密钥。
So my final solution looks like:
所以我的最终解决方案如下:
res.cookie('cookieName', '', {
domain: 'https://my.domain.com',
maxAge: 0,
overwrite: true,
});

