销毁 cookie NodeJs [英] Destroy cookie NodeJs
问题描述
我正在使用 Cookies 模块来设置 cookie.以下是我的代码:
I am using Cookies module for setting cookie. Here is following my code:
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);
但在文档中我还没有找到如何销毁这个 cookie.
But in documentation I haven't found how to destroy this cookie.
如有任何建议,我们将不胜感激.
Any suggestion would be appreciated.
推荐答案
根据 HTTP 规范,没有办法删除 cookie.为了有效地删除"cookie,您可以将到期日期设置为过去的某个日期.本质上,这将为您带来以下结果(根据 cookie 模块文档):
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):
cookies.set('testtoken', {maxAge: 0});
或者根据 HTTP 规范:
Or according to the HTTP specification:
cookies.set('testtoken', {expires: Date.now()});
这两个都应该有效.您可以将 Date.now()
替换为 new Date(0)
以获得真正的旧日期.
Both of which should work. You can replace Date.now()
with new Date(0)
for a really old date.
这篇关于销毁 cookie NodeJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!