Redirect all requests to ExpressJS except index home page?

I have an ExpressJS / AngularJS site on Heroku and I use Express to redirect all non-https requests to the https version.

My problem is - I do not want to redirect the main page - everything else, yes. I want to just serve the homepage without https redirection. Any ideas how to do this with Express?

Thank you very much!

app.get('*',function(req,res,next){ if( req.headers['x-forwarded-proto'] != 'https' ) res.redirect('https://mydomain.com'+req.url) else next() /* Continue to other routes if we're not redirecting */ }) app.use(express.static(__dirname)); app.listen(port); 
+4
source share
2 answers
 app.all('*', function(req, res, next){ if(req.path === '/'){ next(); } else if(req.secure){ next(); } else { var port = 443; // or load from config if(port != 443){ res.redirect('https://'+req.host+':'+port+req.originalUrl); console.log('redirecting to https://'+req.host+':'+port+req.originalUrl); } else { res.redirect('https://'+req.host+req.originalUrl); console.log('redirecting to https://'+req.host+req.originalUrl); }; }; }); 
+4
source
 if (process.env.DEVMODE == 'dev'){ if( req.headers['x-forwarded-proto'] != 'https' ) if !(req.url == homeURL) res.redirect('https://mydomain.com'+req.url) else next() /* Continue to other routes if we're not redirecting */ } else { next(); } }) 
0
source

Source: https://habr.com/ru/post/1493686/


All Articles