I have an application running on Heroku and I'm trying to ensure that the URL always have the https://, even when the user does not provide it.
With the following configuration, I could make it replace http:// for https:// when the user provide http://example.com on the URL. But still doesn't work when the user does not provide http:// (when he provide only example.com):
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);
}
var port = process.env.PORT || 5000;
app.listen(port);
app.all('/*', function(req, res) {
res.sendfile('index.html');
});
console.log('server started '+ port);
How could I achieve that?
EDIT
I'm not having issues with configuring SSL. It's working fine when the user does provide http:// or https:// prefix on the URL. The problem is when the user does not provide this prefix. In this case, the browser does not prepend http:// neither https:// automatically, and then the SSL doesn't apply.