Express.js redirect to HTTPS and send index.html

后端 未结 1 1183
囚心锁ツ
囚心锁ツ 2020-12-29 11:08

I have a simple Express.js instance that\'s serving up static assets for a single page Angular app. I set up some middleware on the Express config so that index.html is retu

相关标签:
1条回答
  • 2020-12-29 11:22

    Heroku terminates SSL connections at the load balancer level, so req.secure will never be true, because your connection to heroku's load balancer is not using SSL, thus creating an infinite redirect loop.

    You have to check the X-Forwarded-Proto header instead:

    if(req.headers["x-forwarded-proto"] === "https"){
      // OK, continue
      return next();
    };
    res.redirect('https://'+req.hostname+req.url);
    

    Edit: you can also set app.enable("trust proxy") to have express check the headers automatically. See http://expressjs.com/guide/behind-proxies.html

    0 讨论(0)
提交回复
热议问题