gpt4 book ai didi

session - Node.js 和 Express session 处理 - 后退按钮问题

转载 作者:IT老高 更新时间:2023-10-28 23:00:25 25 4
gpt4 key购买 nike

我的 Express 应用程序中有一个受限区域“/dashboard”。我用一个很小的函数来限制访问:

app.get('/dashboard', loadUser, function(req, res){
res.render('dashboard', {
username: req.session.username
});
});

function loadUser(req, res, next){
if (req.session.auth) {
next();
} else {
res.redirect('/login');
}
};

问题是当我通过调用注销用户时...

app.get('/logout', function(req, res){
if (req.session) {
req.session.auth = null;
res.clearCookie('auth');
req.session.destroy(function() {});
}
res.redirect('/login');
});

... session 被终止,但是当我在浏览器中点击返回按钮时,我从浏览器的缓存中获得了受限页面。这意味着“/dashboard”上没有 GET,也没有用户登录验证。

我尝试在 meta(Jade 模板)中使用 no-cache,但它仍然不起作用。

meta(http-equiv='Cache-Control', content='no-store, no-cache, must-revalidate')
meta(http-equiv='Pragma', content='no-cache')
meta(http-equiv='Expires', content='-1')

有人吗?

最佳答案

遗憾的是,乔希的回答对我不起作用。但经过一番搜索,我发现了这个问题:What's the best way to deal with cache and the browser back button?

并采用了这个 node.js/express 问题的答案。您只需要更改以下行

res.header('Cache-Control', 'no-cache');

res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');

现在,每次我使用浏览器的后退按钮时,页面都会重新加载而不是缓存。

* express v4.x 更新 *

// caching disabled for every route
server.use(function(req, res, next) {
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
next();
});

// otherwise put the res.set() call into the route-handler you want

关于session - Node.js 和 Express session 处理 - 后退按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6096492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com