标头未使用node.js/Express进行重定向设置

Nat*_*ppi 9 header http-headers node.js express

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

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

重定向的工作方式如下:

res.redirect('/');
Run Code Online (Sandbox Code Playgroud)

这在我的笔记本电脑上工作正常,但在我的手机上,它重定向到'/',处于旧状态,可能是因为缓存.如果我在手机上刷新页面,"/"将会显示出来.

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

尝试过以下两种方式设置标头(单独),但它们似乎没有发送:

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
});
Run Code Online (Sandbox Code Playgroud)

以下是我目前收到的标题:

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
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

非常感谢.

sam*_*tny 10

我发表评论,但我今天刚加入,没有任何声望点.

如果我理解正确,你使用express.static服务页面(所以它只是一个简单的HTML页面),但如果用户已登录,你使用快递的路由器,我是否正确?

我也猜测你把上面提到的代码设置在主页的路径中.

如果是这种情况,您的问题不是浏览器缓存,而是由于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'));
});
Run Code Online (Sandbox Code Playgroud)

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

响应头,我得到没有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
Run Code Online (Sandbox Code Playgroud)

响应头,我得到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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它可以工作,但您可以自己运行此代码.

如果你想运行它,你需要有expressnode_modules或全球的装机量(与-g标志).