Express.js redirect to HTTPS and send index.html
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