Optional force automatic redirections to HTTPS if HTTP is used by mistake #4010
Description
First off, let me use this opportunity to express my gratitude for all your hard work on this awesome project. Speaking of which, now that heroku has become the recommended platform for hosting, there is one thing that bothers me, namely that heroku doesn't support automatic redictions to HTTPS.
If one (by mistake) types HTTP in the URL or if HTTP happens to be the default choice of a given browser and one (again by mistake) doesn't pay attention then the site will be served via HTTP. For some this is fine. Personally, I would prefer either an error code or an automatic redirection to HTTPS. To the best of my knowledge this is not supported directly by the heroku platform so it has to be supported in the app itself. I guess something like this (in app.js) would serve the purpose and optionally support this behaviour:
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
- if (env.settings.isEnabled('httpsredirection')) {
-
console.info('Enabled httpsredirection: redirect http to https');
-
app.use((req, res, next) => {
-
if (req.header('x-forwarded-proto') !== 'https')
-
res.redirect(`https://${req.header('host')}${req.url}`)
-
else
-
next()
-
})
- }
app.set('view engine', 'ejs');
// this allows you to render .html files as templates in addition to .ejs
app.engine('html', require('ejs').renderFile);
That is, if not enabled it work the same way as today, both http:// and https:// will work. If enabled all http:// requests will be directed to similar https:// requests.
Please let me know if you consider this relevant and thanks again for all your hard work on this project. We are many that are truly grateful for your efforts.