Skip to content

Instantly share code, notes, and snippets.

@arcseldon
Created October 14, 2014 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcseldon/d7427075e69596a1adeb to your computer and use it in GitHub Desktop.
Save arcseldon/d7427075e69596a1adeb to your computer and use it in GitHub Desktop.
// author arcseldon@icloud.com
'use strict';
module.exports = function (req, res, next) {
// we ARE deploying to Heroku. SSL termination happens at the load balancer,
// before encrypted traffic reaches your node app. It is possible to test
// whether https was used to make the request with req.headers['x-forwarded-proto'] === 'https'.
// See SOF answer by arcseldon: http://stackoverflow.com/a/22585754/1882064
if ((req.headers['x-forwarded-proto'] !== 'https') && (process.env.NODE_ENV === 'production')) {
return res.redirect([
'https://',
req.get('Host'),
req.url
].join(''));
} else {
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment