I'm having some problems forcing SSL on my node.js app that is deployed to Heroku. I want to force HTTPS in all scenarios. I am also using Express.
I have followed the code on this post: Heroku NodeJS http to https ssl forced redirect (the answer from arcseldon).
I'm testing in Chrome.
Here is my test scenario: If I go to www.mywebsite.com, it re-directs to the https://www.mywebsite.com -> This is working as I expect.
Now I am in https://www.mywebsite.com. If I go into the URL in Chrome and edit the 'https' to just 'http' and it hit enter. It is making the connection just via http (I checked in Chrome developer tools).
Here is my snippets of my code in case I am doing something wrong.
var forceSsl = function (req, res, next) {
if ( req.headers['x-forwarded-proto'] != 'https'){
console.log( 'forceSSL req.get = ' + req.get('Host') + ' req.url = ' + req.url );
return res.redirect('https://' + req.get('Host') + req.url );
} else {
console.log( 'No need to re-direct to HTTPS' );
next();
}
};
if ('development' == app.get('env')) {
console.log('Started in dev mode');
// Other code here
} else if ('production' == app.get('env')) {
console.log('Started in PROD mode');
app.use(forceSsl);
app.use('/public', express.static(__dirname + '/public'));
app.use(express.errorHandler());
mongoose.connect(process.env.MONGOHQ_URL);
}
On a side note, I never see the console messages in the 'forceSsl' function.
I have configured my domain to point to the SSL version of the heroku app so I don't know how it is making a connection via HTTP.
Thank you very much in advance for your help.
Edit: In the above test case where I think it is working in Chrome, I think Chrome is intervening and re-directing so it is probably not working as expected.
When I test via curl, I can see that the site accepts a request via HTTP and does not do a re-direct.