不使用node.js / Expressredirect的标题

我对node.js,Express和移动开发相对比较陌生,并且遇到了一个我认为与使用Express发送头文件有关的问题。

用户从主页“/”开始,未login,然后单击button进入login页面。 当他们提交他们的用户名和密码到“/ validate_signin”时,他们应该被redirect回主页,这次主页以不同的方式显示,因为他们已经login了。

redirect是这样工作的:

res.redirect('/'); 

这在我的笔记本电脑上工作正常,但在我的手机上,它redirect到“/”,在旧的状态,大概是因为caching。 如果我刷新手机上的页面,则会显示“/”。

我发现这篇文章: 如何在所有浏览器上控制网页caching?

试图设置以下两种方式(单独)的标题,但他们似乎并没有发送:

 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 }); 

这是我目前收到的标题:

 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 

有任何想法吗?

非常感谢。

我会评论,但我今天刚join,没有任何声望点。

如果我理解正确的话,你使用express.static来提供页面(所以这只是一个简单的HTML页面),但是如果用户login,你使用express的路由器,我是否正确?

我也猜你把提到的代码设置标题在主页的路线。

如果是这样的话,你的问题不是浏览器caching,这是由于connect中间件的性质。

中间件在链中执行,当它们完成时调用下一个中间件,这意味着,如果我正确假设,express.static在路由器之前被调用,并且它仅仅提供静态HTML页面。

所以你的路由永远不会执行,因为express.static不会调用next() (由于一个显而易见的原因,文件存在)。

希望我假设正确。


编辑:

看起来我错了。 你确实说过在你的笔记本电脑上工作的很好,所以它看起来像一个客户端caching问题。

我仍然不确定如何使用express.static()显示不同的主页,或者在上面提到的代码放置位置,我将在不看到代码的情况下给出一个代码,但可能需要我别人为了帮助你。

这些响应头文件应该在第一个响应中设置(当你访问主页时),这与redirect没有任何关系。 现在让我们把redirect部分旁白。

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

 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')); }); 

此代码指示我的浏览器不要caching页面。

没有 noCachePlease中间件的响应头文件:

 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 

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_modulesnode_modules或全局安装(使用-g标志)。