4

我在 Heroku 上托管了一个 ExpressJS/AngularJS 站点,我使用 Express 将所有非 https 请求重定向到 https 版本。

我的问题是 - 我不想重定向主页 - 其他一切,是的。我只想在没有 https 重定向的情况下提供主页。任何想法如何用 Express 做到这一点?

非常感谢!

app.get('*',function(req,res,next){
    if( req.headers['x-forwarded-proto'] != 'https' )
        res.redirect('https://mydomain.com'+req.url)
    else
        next() /* Continue to other routes if we're not redirecting */
})

app.use(express.static(__dirname));
app.listen(port);
4

2 回答 2

4
app.all('*', function(req, res, next){
  if(req.path === '/'){
    next();
  } else if(req.secure){
    next();
  } else {
    var port = 443;  // or load from config
    if(port != 443){
      res.redirect('https://'+req.host+':'+port+req.originalUrl);
      console.log('redirecting to https://'+req.host+':'+port+req.originalUrl);
    } else {
      res.redirect('https://'+req.host+req.originalUrl);
      console.log('redirecting to https://'+req.host+req.originalUrl);
    };
  };
});
于 2013-07-26T14:45:14.780 回答
0
if (process.env.DEVMODE == 'dev'){
    if( req.headers['x-forwarded-proto'] != 'https' )
        if !(req.url == homeURL)
           res.redirect('https://mydomain.com'+req.url)
    else
        next() /* Continue to other routes if we're not redirecting */
  } else {
        next();
  }
})
于 2013-07-26T14:06:45.430 回答