问题
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