我正在Heroku上托管ExpressJS / AngularJS网站,我正在使用Express将所有非https请求重定向到https版本。
我的问题是 - 我不想重定向主页 - 其他一切,是的。我想在没有https重定向的情况下提供主页。有任何想法如何使用Express?
非常感谢!
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);
答案 0 :(得分:4)
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);
};
};
});
答案 1 :(得分:0)
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();
}
})