【问题标题】:Redirecting 301 using express and node.js. GET not being called使用 express 和 node.js 重定向 301。 GET 没有被调用
【发布时间】:2014-10-23 18:17:03
【问题描述】:

我正在尝试做一个简单的 301 重定向。我的代码如下所示:

app.get('/',function(req,res){
  console.log(req.get('host')+req.originalUrl);
  if(req.get('host')+req.originalUrl == "facebook.com:3000/"){
    res.redirect(301,'https://youtube.com');
  } else {
    res.redirect(301,'https://google.com');
  }
});

所以我所做的是编辑我的主机文件,以便将 facebook.com 链接到 localhost (127.0.0.1) 问题是我的app.get() 没有被调用,除了几次(通常是当我有一段时间不查看 app.js 文件然后再次使用它时)。在这些时间之后,即使我编辑文件以将其重定向到其他网站,它也将始终重定向到同一个网站。除此之外,还有一些相关代码:

app.locals.cacheBuster = Date.now();
app.set('port', process.env.PORT || argv.p || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, 'public/img/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(express.session({
  secret: 'your secret code',
  store: new MongoStore({
    db: mongoose.connection.db,
    auto_reconnect: true
  })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
  res.locals({
    user: req.user,
    cookies: req.cookies,
    pullup: { // global client-side JS object
      baseUrl: req.protocol + '://' + req.get('host')
    }
  });

  if (!_.isObject(req.session.flash) || !Object.keys(req.session.flash).length) {
    req.session.windowscrolly = 0;
  }
  if (req.body.windowscrolly) req.session.windowscrolly = req.body.windowscrolly;
  res.locals.windowscrolly = req.session.windowscrolly;
  res.setHeader("Content-Security-Policy", "script-src 'self' https://apis.google.com http://sysinct.herokuapp.com; frame-src 'self' https://gitter.im;");
  res.setHeader("X-Frame-Options", "DENY");
  next();
});

app.use(flash());

app.use(less({ src: __dirname + '/public', compress: true }));

app.use(app.router);

app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));

app.use(function(req, res) {
  res.render('404', { status: 404 });
});

app.use(function(err, req, res, next){
  console.error("req: "+req +"\nerror:"+err.stack);
  res.statusCode = 500;
  res.render('error',{error:err});
});

app.locals.timeago = timeago;

...最终,

app.listen(app.get('port'), function() {
  console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});

我已经尝试将我的 app.get 函数插入到这段代码周围的各个地方(我知道我现在没有在这个示例中放置 app.get(),因为我不知道放在哪里)因为我读到它与app.use(app.router) 和其他中间件的顺序很重要,但是我没有找到为什么不调用app.get() 的运气。

  1. 我想知道为什么没有调用 app.get()
  2. 如果我的重定向方式是正确的。 谢谢!

【问题讨论】:

    标签: javascript node.js redirect express http-status-code-301


    【解决方案1】:

    对于 301 重定向,chrome 浏览器会将 301 重定向的信息存储在缓存中(以便服务器以后可以更快地加载数据)。所以我的 app.get 没有被调用,因为缓存正在提供网页的本地化版本,而不是再次从服务器请求。 要使其正常工作,请清除设置中的缓存(浏览历史记录所在的位置)。然后,它将通过相应地请求 Web 服务器进行重定向,并调用 app.get()。

    【讨论】:

      【解决方案2】:

      你是否设置在顶部:

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

      ?

      我知道以前做过类似的工作

      var bodyParser = require('express.bodyParser()');

      但不再是,现在与 express 分开了,我看到你有几个这样的模块,不是吗?

      【讨论】:

      • 是的,我将快递设置在顶部。您能否详细说明您的第二条评论?感谢您的帮助。
      • 知道了。不是一个明确的问题,而是浏览器缓存的问题。
      猜你喜欢
      • 2011-01-28
      • 2018-03-09
      • 1970-01-01
      • 2012-07-13
      • 2014-07-31
      • 2013-09-19
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多