gpt4 book ai didi

node.js - header 未使用 node.js/Express 设置重定向

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

我对 node.js、Express 和移动开发相对较新,并且遇到了一个我认为与使用 Express 发送 header 有关的问题。

用户从主页“/”开始,未登录,然后单击按钮进入登录页面。当他们将用户名和密码提交到“/validate_signin”时,他们应该被重定向回主页,这一次主页显示不同,因为他们已登录。

重定向是这样工作的:

res.redirect('/');

这在我的笔记本电脑上运行良好,但在我的手机上,它在旧状态下重定向到“/”,大概是因为缓存。如果我在手机上刷新页面,'/' 会正常显示。

我发现了这个帖子: How to control web page caching, across all browsers?

已尝试通过以下两种方式(分别)设置 header ,但它们似乎没有发送:

res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
res.header("Expires", 0);

res.writeHead(302, {
"location": "/",
"Cache-Control" : "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": 0
});

这是我目前收到的 header :

HTTP/1.1 304 Not Modified
X-Powered-By: Express
Date: Fri, 13 Jul 2012 17:35:18 GMT
Cache-Control: public, max-age=0
Last-Modified: Fri, 13 Jul 2012 12:32:12 GMT
Etag: "3223-1342182732000"
Accept-Ranges: bytes
Connection: keep-alive

有什么想法吗?

非常感谢。

最佳答案

我想发表评论,但我今天才加入,没有任何声望点。

如果我理解正确,您使用 express.static 提供页面(因此它只是一个纯 HTML 页面)但如果用户已登录,您使用 express 的路由器,对吗?

我也猜你把提到的代码设置在主页的路由中。

如果是这种情况,您的问题不是浏览器缓存,而是由于 connect 中间件的性质而发生的。

中间件在链中执行,完成后调用下一个,这意味着,如果我猜对了,express.static 在您的路由器之前被调用,它只是为静态 HTML 页面提供服务。

所以你的路由永远不会被执行,因为 express.static 不会调用 next() (原因很明显,文件存在)。

希望我猜对了。


编辑:

看来我猜错了。您确实说过它在您的笔记本电脑上运行良好,因此它看起来肯定是客户端缓存问题。

我仍然不确定您如何使用 express.static() 显示不同的主页,或者您将上面提到的代码放在哪里,我将在没有看到您的代码的情况下试一试,但可能需要它为我和其他人提供帮助。

这些响应头应该设置在第一个响应中(当您访问主页时),它与重定向无关。现在让我们把重定向部分放在一边。

我写了一个快速(快速)的例子:

var express = require('express'),
http = require('http')
app = express();

app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));

/*
* Here's where I set the headers, make sure it's above `express.static()`.
*
* Note: You can safely ignore the rest of the code, (it's pretty much "stock").
*/
app.use(function noCachePlease(req, res, next) {
if (req.url === '/') {
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
res.header("Expires", 0);
}

next();
});

app.use(express.static(__dirname + '/public'));
});

app.configure('development', function() {
app.use(express.errorHandler());
});

http.createServer(app).listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});

此代码指示我的浏览器不要缓存该页面。

我得到没有 noCachePlease 中间件的响应 header :

Accept-Ranges   bytes
Cache-Control public, max-age=0
Connection keep-alive
Content-Length 5
Content-Type text/html; charset=UTF-8
Date Fri, 20 Jul 2012 19:25:38 GMT
Etag "5-1342811956000"
Last-Modified Fri, 20 Jul 2012 19:19:16 GMT
X-Powered-By Express

我得到的响应头 with noCachePlease 中间件:

Accept-Ranges   bytes
Cache-Control no-cache, no-store, must-revalidate
Connection keep-alive
Content-Length 5
Content-Type text/html; charset=UTF-8
Date Fri, 20 Jul 2012 19:26:08 GMT
Etag "5-1342811956000"
Expires 0
Last-Modified Fri, 20 Jul 2012 19:19:16 GMT
Pragma no-cache
X-Powered-By Express

如您所见,它可以工作,但您可以自己运行此代码。

如果你想运行它,你需要在 node_modules 下有 express 或全局安装(使用 -g 标志) .

关于node.js - header 未使用 node.js/Express 设置重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475682/

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