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
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