I can't delete cookies in Express

房东的猫 提交于 2019-12-12 04:59:49

问题


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.

I've also tried res.clearCookie('TEST', {path: '/'}); to no avail.

I'm including cookie-parser like this:

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

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

I would appreciate any help that I can get. Thanks.


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/46130677/i-cant-delete-cookies-in-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!