没有使用node.js / Express设置重定向的标题 [英] Headers not setting with redirect using node.js / Express

查看:190
本文介绍了没有使用node.js / Express设置重定向的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对node.js,Express和移动开发相对较新,遇到了一个问题,我认为与Express发送头文件有关。



用户从主页/开始,未登录,然后点击按钮进入登录页面。当他们将用户名和密码提交到/ validate_signin时,他们应该被重定向回主页,此时主页的登录状态不一样。



重定向的工作方式如下:

  res.redirect('/'); 

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



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



已尝试以下列两种方式(单独)设置标题,但似乎没有发送:

  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未修改
X-Powered by:Express
日期:Fri,13七月2012 17:35:18 GMT
缓存控制:public,max-age = 0
最后修改:Fri,13七月2012 12:32:12 GMT
Etag:3223-1342182732000
接受范围:字节
连接:保持活着

任何想法?



非常感谢。

解决方案

我会评论,但我刚加入今天没有任何信誉点。



如果我理解正确,您正在使用express.static(所以它只是一个纯HTML页面)提供的页面,但如果用户签名 - 你使用快递的路由器,我是否正确?



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



如果是这样,你的问题不是浏览器缓存,它是由于nat connect middlewares。



中间件在链中执行,完成后调用下一个意思是,如果我假设正确,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'));

/ *
*这里是我设置标题的地方,确保它在`express.static()'之上
*
*注意:您可以安全地忽略其余的代码(它几乎是库存)
* /
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 中间件:

  Accept-Ranges bytes 
Cache-Control public,max-age = 0
连接keep-alive
内容长度5
内容类型为text / html; charset = UTF-8
日期星期五,20七月2012 19:25:38 GMT
Etag5-1342811956000
最后修改Fri,20七月2012 19:19:16 GMT
X-Powered by By Express

我得到的 noCachePlease 中间件:

  Accept-Ranges bytes 
cache-control no-cache,no-store,must-revalidate
连接keep-alive
Content-Length 5
内容类型text / html; charset = UTF-8
日期星期五,20七月2012 19:26:08 GMT
Etag5-1342811956000
过期0
最后修改Fri,20七月2012 19 :19:16 GMT
Pragma no-cache
X-Powered by By Express

所以你可以看到,它的作品,但你可以自己运行这个代码。



如果你想运行它,你需要有$ code>表达在 node_modules 下或全局安装(使用 -g 标志) / p>

I am relatively new to node.js, Express, and mobile development, and have run into a problem that I think has to do with sending headers with Express.

The user starts at the home page '/', not logged in, then clicks a button to go to the sign-in page. When they submit their username and password to '/validate_signin', they should be redirected back to the home page, this time with the home page showing up differently because they are logged in.

The redirection worked like this:

res.redirect('/');

This works fine on my laptop, but on my mobile phone it redirects to '/', in its old state, presumably because of caching. If I refresh the page on the phone, '/' will show up as it should.

I found this post: How to control web page caching, across all browsers?

Have tried to set headers in the following two ways (separately), but they don't seem to be sending:

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

Here are the headers that I am currently receiving:

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

Any ideas?

Many thanks.

解决方案

I'd comment but I just joined today and don't have any reputation points.

If I understand it correctly, you're serving the page using express.static (so it's just a plain HTML page) but if the user is signed-in, you use express's router, am I correct?

I'm also guessing you put the mentioned code to set the headers in the homepage's route.

If that's the case, your issue isn't browser-caching, it occurs due to the nature of connect middlewares.

Middlewares execute in a chain, calling the next one when they're done, which means, if I assumed correctly, express.static is called prior to your router and it simply serve the static HTML page.

So your route is never executed because express.static won't call next() (for an obvious reason, the file exists).

Hope I assumed right.


Edit:

Looks like I assumed wrong. You did say it works fine on your laptop so it definitely looks like a client-side caching issue.

I'm still not sure how exactly you display a different homepage using express.static() or where you place the code you mentioned above, I'm going to give it a shot without seeing your code but it may be needed for me and others in order to help you out.

Those response headers should be set in the first response (when you visit the homepage), it has nothing to do with the redirect. Let's put the redirect part asides, for now.

I wrote a quick (express) example:

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

This code instructs my browser not to cache the page.

The response headers I get without the noCachePlease middleware:

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

The response headers I get with the noCachePlease middleware:

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

So as you can see, it works but you could run this code yourself.

If you want to run it, you'll need to have express under node_modules or installed globally (with the -g flag).

这篇关于没有使用node.js / Express设置重定向的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆