I'm trying to redirect to an https connection if requests come in on http.
I've done this before on heroku using the X-Forwarded-Proto field that
express will fall back on as well. However, when I'm on my local machine,
my https requests think they're still http. Chrome is giving me the red X
through my lock on the address bar, but according to the description it
sounds like my connection is still with TLS and secure, just that it can't
verify my certificate with a third party(as expected, I generated it
myself). Does anyone know what's going on?
(I know this won't actually redirect to port 3001 as I need it to as well,
however when I hit https://localhost:3001 it redirect loops and then Chrome
aborts, I'd like to fix that behavior if possible as it
thinks https://localhost:3001 is http for some reason).
Here's my code:
app.get('*', function(req, res, next) {
if(req.protocol == 'http') {
console.log('redirecting');
res.redirect('https://' + req.headers.host + req.url);
} else {
console.log('next');
next();
}
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
spdy.createServer(https.Server, options, app).listen(app.get('port') + 1,
function(){
console.log("Express server listening on port " + (app.get('port') + 1));
});