I am adding HTTPS to a node.js application, while forwarding HTTP to HTTPS. I am using the following bit of code:
// Trusting proxy
self.app.enable('trust proxy');
// Http -> Https redirection middleware
self.app.use(function (req, res, next) {
var schema = req.protocol.toLowerCase();
console.log("Redirection check, schema: " + schema);
if (schema === 'https') {
console.log("Next");
next();
} else {
var tmp = 'https://' + req.headers.host + req.url;
console.log("Redirect to: " + tmp);
res.redirect(tmp);
}
});
All works fine when I browse https://localhost:8443/index.html, the page opens fine.
But when I try http://localhost:8443/index.html, Chrome seems to redirect or rewrite the url to localhost:8443/index.html and Chrome waits for localhost eternally. There is no console message from my application. I am on under windows 7 with Chrome.
What is wrong with my code?
Chrome is just hiding the http:// portion of the URL, this is normal.
You can't run http and https on the same port.
You've specified http in the address, so the browser tries to connect with HTTP. The server is listening for HTTPS so it fails.