Node.js 节点和Angular 2应用程序中的HTTP到HTTPS重定向

Node.js 节点和Angular 2应用程序中的HTTP到HTTPS重定向,node.js,express,redirect,angular2-routing,Node.js,Express,Redirect,Angular2 Routing,我有一个在80和443端口上运行的应用程序。当用户点击http版本应用程序时,它应该被重定向到HTTPS。我已经尝试了下面的代码来做这件事 function handleRedirects(req, res, next) { if (!req.secure) { return res.redirect('https://' + req.get('host') + req.url); } next(); } app.use(handleRedirects)

我有一个在80和443端口上运行的应用程序。当用户点击http版本应用程序时,它应该被重定向到HTTPS。我已经尝试了下面的代码来做这件事

function handleRedirects(req, res, next) {
    if (!req.secure) {
        return res.redirect('https://' + req.get('host') + req.url);
    }
    next();
}

app.use(handleRedirects);

https.createServer(credentials, app).listen(443, function() {
    console.log('App is running on port 443');
});

// all other routes are handled by Angular
app.get('/*', function(req, res) {
    console.log("Default handler\n\n\n\n")
    res.sendFile(path.join(__dirname, '/../../dist/index.html'));
});

app.listen(80, function() {
    logger.info('App is running on port 80');
    admins.refresh();
});
所以,当应用程序启动时,如果我点击localhost,它应该会被重定向到。但它并没有像预期的那样工作。
代码中有什么错误。我参考了

我只想设置https,以便它只在生产环境中重定向,我在我的站点上使用的代码如下

module.exports.httpsRedirect = function(req,res,next){
 if(req.headers['x-forwarded-proto'] != 'https' && process.env.NODE_ENV === 'production')
    res.redirect('https://'+req.hostname+req.url)
 else
    next() /* Continue to other routes if we're not redirecting */
};

下面的代码解决了我的问题

var app = express();
app.get('/refresh', function (req, res) {
    res.send(200);
});
https.createServer(credentials, app).listen(443, function () {
  console.log('App is running on port 443');
});


var http = express();

http.get('/', function (req, res) {
  res.redirect('https://' + req.get('host') + req.url);
})
http.listen(80, function () {
  logger.info('App is running on port 80');
});