[英]server sent cookie is not overriding the existing cookie
i have a express
application and using cookie-session
module for session management. 我有一个express
应用程序,并使用cookie-session
模块进行会话管理。 application has 2 paths https://example.com/abc/def
and https://example.com/abc/ghi
. 应用程序具有2个路径https://example.com/abc/def
和https://example.com/abc/ghi
。 if i am visiting any path first then it sets a cookie but if i am changing the URL to other path then i can see that server is responding with new value for the cookie in developer console but it is not getting updated in browser. 如果我先访问任何路径,那么它将设置一个cookie,但是如果我将URL更改为其他路径,那么我可以看到服务器在开发人员控制台中使用cookie的新值进行响应,但它在浏览器中未得到更新。 any idea what is preventing cookie from getting updated? 知道什么阻止cookie更新吗?
You need to clearCookie before you set new one. 您需要先清除Cookie,然后再设置一个。 Most importantly cookies work with domain not the paths. 最重要的是,Cookie使用域而不是路径。 So in both the path where you want to set cookie you have to check for existing cookie and if you found one you have to remove it to set new one. 因此,在要设置cookie的路径中,都必须检查现有的cookie,如果找到了它,则必须将其删除以设置新的cookie。
const cookieSession = require('cookie-session');
const express = require('express');
const app = express();
app.set('trust proxy', 1) // trust first proxy
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2']
}));
app.get('/abc', function(req, res, next) {
req.session = {
'views':'abc'
};
res.end(req.session.views + ' cookie value is set');
});
app.get('/xyz', function(req, res, next) {
req.session = {
'views':'xyz'
};
res.end(req.session.views + ' cookie value is set');
});
app.get('/test', function(req, res, next) {
res.end(req.session.views + ' cookie found');
});
app.listen(3000);
This is sample code where path /abc
and /xyz
sets diffrent values for session and those values can be seen on /test
path. 这是示例代码,其中路径/abc
和/xyz
设置会话的不同值,并且这些值可以在/test
路径上看到。
So if you first hit /abc
route and than hit /test
path you will get cookie value {'views': 'abc'}
and if you hit /xyz
and than hit /test
cookie value will be {'viewa':'xyz'}
; 因此,如果您首先命中/abc
路由,然后命中/test
路径,您将获得Cookie值{'views': 'abc'}
;如果您命中/xyz
然后命中/test
cookie值将为{'viewa':'xyz'}
;
在进一步分析中,我发现Cookie的内容长度超出了4096字节的允许大小,一旦我们修复了内容,我们就可以正确地设置Cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.