title: Redirect http to https express.js
body: <p>I'm trying to reroute <code>http (80)</code> to <code>https (443)</code> in my express app.  I'm using some middle ware to do this. If i go to my <code>https://my-example-domain.com</code>, everything is fine.  But if I go to <code>http://my-example-domain.com</code> it does not redirect and nothing shows up.</p>
  
  <p>I also have setup some iptables on my ubuntu server</p>
  
  <pre><code>sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
  sudo iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
  sudo iptables -A INPUT -i eth0 -p tcp --dport 8443 -j ACCEPT
  sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443
  
  
  
  
  function requireHTTPS(req, res, next) {
    if (!req.secure) {
      return res.redirect('https://' + req.headers.host + req.url);
    }
    next();
  }
  
  // all environments
  app.set('port', process.env.PORT || 8443);
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  app.use(express.logger('dev'));
  app.use(requireHTTPS);  // redirect to https
  app.use(express.json());
  app.use(express.urlencoded());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.get('/', function(req, res){
      res.render('index');
  })
  
  https.createServer(options, app).listen(8443);
  </code></pre>
  
  <p>So my question is do I just need to add another <code>iptables</code> rule?  Or do I need to configure something in my app?</p>
  
  <p>So based on one answer below, I don't think its a middleware problem, but a port issue.  For example: if i go to <a href="http://my-example-domain.com" rel="nofollow">http://my-example-domain.com</a>, doesn't work. But if I add port 8443,<a href="http://my-example-domain.com:8443" rel="nofollow">http://my-example-domain.com:8443</a>, it redirects fine.</p>