diff --git a/index.js b/index.js index d8953b6b..86077054 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,8 @@ if (process.env.NODE_ENV !== 'production') { app.use(morgan('dev')); } +// https://stackoverflow.com/questions/10348906/how-to-know-if-a-request-is-http-or-https-in-node-js +app.enable('trust proxy'); app.use(hostnameRedirection); app.use(httpsEnforcement); diff --git a/server/https-enforcement.js b/server/https-enforcement.js index 560bb4f1..9b177b9c 100644 --- a/server/https-enforcement.js +++ b/server/https-enforcement.js @@ -3,7 +3,7 @@ export default function httpsEnforcement (req, res, next) { console.log(req.protocol); // Use HTTPS if not on localhost - if (req.protocol !== 'https' && !['localhost', '127.0.0.1'].includes(hostname)) { + if (!req.secure && !['localhost', '127.0.0.1'].includes(hostname)) { return res.redirect(301, `https://${hostname}${req.originalUrl}`); }