I’m using express to serve static files on my vps. I created two servers http and https like so:
var httpServer = http.createServer(app);
httpServer.listen(port); // 3000
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(3001);
They are redirected to correct ports via iptables but this is not the point.
By using a middlewear like below:
app.use(function (req, res, next) {
!req.secure
? res.redirect(301, path.join('https://', req.get('Host'), req.url))
: next();
});
All my requests are well redirected. However, when loading my website with only the domain without ssl (http://example.com) and without any child routes (like http://example.com/contact), this is not redirecting to https.
Can you help me to find out what I missed? If it is an issue or not?
Thank you. (I logged that question in stackoverflow as well: https://stackoverflow.com/questions/46024470/nodejs-express-redirection-to-https-not-working-for-homepage)
I finally found out what was the issue: I was serving the public folder before the security test…
So here are the steps now:
Now it is working perfectly!