title: HTTP to HTTPS redirection in Node and Angular 2 App
body: <p>I have an app which runs on 80 and 443 port. When user hit http version application, it should get redirected to HTTPS. I have tried the below code to do this.</p>
  
  <pre><code>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();
  });
  </code></pre>
  
  <p>So when app started, if I hit <strong>localhost</strong> it should get redirected to <a href="https://localhost" rel="nofollow noreferrer">https://localhost</a>. But its not working as expected.
  Whats wrong in the code. I have refferred <a href="https://stackoverflow.com/questions/15813677/https-redirection-for-all-routes-node-js-express-security-concerns">HTTPS redirection for all routes node.js/express - Security concerns</a></p>