简体   繁体   English

强制将Heroku / Angular应用重定向到HTTPS版本

[英]Force redirect heroku/angular app to HTTPS version

I have an angular 5 app which is hosted on heroku. 我有一个在heroku上托管的angular 5应用程序。 Currently the users can access the HTTP version of the app. 当前,用户可以访问应用程序的HTTP版本。

How can I force the users to be redirected to HTTPS version even if they access HTTP? 即使用户访问HTTP,如何强制用户重定向到HTTPS版本?

What I have tried: 我试过的

app.use(function (req, res, next) {
let sslUrl;

if (process.env.NODE_ENV === 'production' &&
    req.headers['x-forwarded-proto'] !== 'https') {

    sslUrl = ['https://myapp.herokuapp.com', req.url].join('');
    return res.redirect(sslUrl);
}

return next();
});

I have placed the above code in my node.js server but it has not worked. 我已经将上面的代码放置在我的node.js服务器中,但是没有用。

The users are unable to use the app over HTTP as they get a 403 ERROR 用户收到403错误时,无法通过HTTP使用该应用程序

I used the "force-ssl-heroku" package at https://github.com/rangle/force-ssl-heroku , works like magic and very easy integrated. 我在https://github.com/rangle/force-ssl-heroku上使用了“ force-ssl-heroku”包,其工作原理类似于魔术,并且非常易于集成。

Just require inside your start entry point script: 只需在您的开始入口点脚本中输入以下内容即可:

var forceSsl = require('force-ssl-heroku');

and use it like that: 并像这样使用它:

app.use(forceSsl);

Deploy and enjoy. 部署并享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM