我无法在Express中删除Cookie [英] I can't delete cookies in Express

查看:518
本文介绍了我无法在Express中删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 res.cookie('TEST','Test content。')。status(200).send('Cookie set。'); 我看到它在浏览器中正确设置。但是,在另一页上,如果我执行 res.clearCookie('TEST')。status(200).send('Cookie已清除。'); 我看到了cookie未删除。我想完全删除此cookie。

If I set a cookie using res.cookie('TEST', 'Test content.').status(200).send('Cookie set.'); I see that it is correctly set in the browser. However, on another page, if I do res.clearCookie('TEST').status(200).send('Cookie cleared.'); I see that the cookie is not deleted. I would like to completely remove this cookie.

我也尝试过 res.clearCookie('TEST',{path:'/'} ); 无济于事。

我加入了cookie解析器,如下所示:

I'm including cookie-parser like this:

const cookieParser = require('cookie-parser');
app.use(cookieParser(process.env.secret));

上图显示了Cookie被清除后看到的内容-但实际上并未删除。

The above image shows what I see after the cookie has been 'cleared' - but it's not actually deleted.

我将不胜感激。谢谢。

推荐答案

服务器无法指示客户端删除特定的cookie。

A server can't instruct a client to remove a particular cookie.

它所能做的就是覆盖cookie,使其值为空,并设置已经过去的到期日期(这正是 res的含义。 clearCookie 确实),希望浏览器能够获取提示并实际上将其删除。

All it can do is to overwrite the cookie so its value is empty, and set an expiry date that has already passed (which is exactly what res.clearCookie does), in the hope that the browser will take the hint and actually remove it.

在您的情况下,即使cookie具有已过期,您的浏览器由于某种原因没有将其删除。 Express无法解决此问题。

In your case, even though the cookie has expired already, your browser isn't removing it for some reason. That's not something Express can fix.

这篇关于我无法在Express中删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆