Forcing HTTPS on Heroku using NodeJS

Hi,

I tried changing redirect to send and the correct destination was showing in the browser.

I couldnt get the req.get to provide any data . I did notice from the {{$_SERVER}} command that Apple suggested I try that the
'x-forwarded-proto'
should be(?)
'HTTP_X_FORWARDED-PROTO'

I made the change and still get the same error.

I'm a bit stumped.

regards,

Nigel

Hi
After a lot of trial and error it seems to work if I add an Else clause to the HTTPS check - see below

exports.before = function(app) {
app.use((req, res, next) => {

if ((req.get('x-forwarded-proto') || '').startsWith('https')) {
  next();
} else {

	if (req.method === 'GET' || req.method === 'HEAD') {
	  res.redirect(301, 'https://' + req.hostname + req.originalUrl);


	} else {
	  res.status(403).send('Please use HTTP when submitting data to this server');
	}
}

});
};

Regards,

Nigel

Ah, that was a stupid mistake in my code, it should exit (return) the function after the next() is called otherwise it will always execute the redirect code.