I have installed a SSL certificate on my page, which runs a Node.js + Express application, configured the Express server to always force redirection to HTTPs and everything works fine, except that the https redirection only works when the page is reloaded or when the Enter key is pressed again. I recorded a gif to show what happens:
And here's my Express configuration.
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
var forceSsl = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
app = express();
app.use(serveStatic(__dirname));
if(process.env.NODE_ENV === 'production') {
app.use(forceSsl);
}
app.all('/*', function(req, res) {
res.sendfile('index.html');
});
var port = process.env.PORT || 5000;
app.listen(port);
console.log('server started '+ port);
My application runs on Heroku. Can anyone help me finding out what's happening?
Thanks in advance.