title: Nodejs res.redirect not working?
body: <p>I have my Angularjs website and I am trying to route all my <code>Http</code> traffic to <code>Https</code> and for that, I have written below code but it is not working, every time I am opening my website on <code>Http</code> redirect is not happening. </p>
  
  <pre><code>var express = require('express');
  var app = express();
  
  app.use(express.static(__dirname + '/public')); // Website part added
  var bodyParser = require('body-parser')
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }))
  
  app.use(function(req,res,next) {
  
      if(req.headers["x-forwarded-proto"] == "http") {
  
          res.redirect("https://" + req.headers.host + req.url);
          return next();
      } else {
          console.log('Request was not HTTP');
          return next();
      } 
  });
  
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  app.get('*', function(req, res) {
      res.sendFile(__dirname + '/public/index.html');
  });
  </code></pre>
  
  <p>Can anyone please tell me why this is not working ??</p>
  
  <p><strong>Edit</strong> - I noticed that whenever I am opening my website then <code>most of the times</code> this middleware is <code>not getting called</code>. How can this be possible ??</p>
  
  <p><strong>Edit</strong> - I have my set <code>base href="/"</code> in my <code>index.html</code> will it cause any problem.</p>