title: Creating an Express JS 4.0 application with https on Openshift, including http redirection
body: <p>Following another <a href="https://stackoverflow.com/questions/25630353/openshift-node-js-do-i-need-to-load-ssl-certificates-in-the-web-console-or-in-t">SO question</a>, the latest I have been trying is (see <a href="http://ligatures.net" rel="nofollow noreferrer">ligatures.net</a>):</p>
  
  <pre><code>self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
  self.port      = process.env.OPENSHIFT_NODEJS_PORT || 443;
  
  if (typeof self.ipaddress === "undefined") {
      self.ipaddress = "127.0.0.1";
  };
  
  ...
  self.app = express();  // 4.8.7
  
  ...
  // Trusting Openshift proxy
  self.app.enable('trust proxy');
  
  // Http -&gt; Https redirection middleware
  self.app.use(function (req, res, next) {
  
      if ( !req.secure() ) {
          var tmp = 'https://' + process.env["DOMAIN_NAME"] + req.originalUrl;
          console.log("Redirect to: " + tmp);
          res.redirect(tmp);
      } else {
          next();
      }               
  
  });
  
  ...
  
  // Creating a http server
  https.createServer(self.app).listen(self.port, self.ipaddress,
      function(err) {
          if (err) {
              console.log("Node server error: " + err.toString());
          } else {
              console.log('%s: Node server started on %s:%d ...',
                  Date(Date.now() ), self.ipaddress, self.port);
          }
  });
  </code></pre>
  
  <p>In the Openshift logs, I get:</p>
  
  <blockquote>
    <p>Property 'secure' of object # is not a function</p>
  </blockquote>
  
  <p>This is the line: <code>if ( !req.secure() ) {</code></p>
  
  <p>The certificates are loaded in the console. The application starts successfully on port 8080.</p>
  
  <p>Why am I getting this message and how should I create a secured Express 4.0 https application in OpenShift? Does anyone have operational code to share? Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have updated the redirection as following:</p>
  
  <pre><code>// Http -&gt; Https redirection middleware
  self.app.use(function (req, res, next) {
  
      if ( req.headers['x-forwarded-proto'] === 'http' ) { 
  
          var tmp = 'https://' + req.headers.host + req.originalUrl;
          console.log("SERVER redirect to: " + tmp);
          res.redirect(tmp);
  
      } else {
  
          var pt = req.protocol || "";
          var ho = req.headers.host || "";
          var ur = req.originalUrl || "";
  
          console.log("\nProtocol: " + pt
                    + "\nHost   : " + ho
                    + "\nUrl    : " + ur);
  
          var tmp = req.protocol + '://' + req.headers.host + req.originalUrl;
          console.log("SERVER no redirect: " + tmp);
          next();
  
      }
  </code></pre>
  
  <p>I see a couple of the following from the console:</p>
  
  <pre><code>SERVER no redirect: http://undefined/
  Protocol: http
  Host   :
  Url    : /
  </code></pre>
  
  <p>and my application still does not work. It looks like a bug to me.</p>
  
  <p>I have opened an issue: <a href="https://bugzilla.redhat.com/show_bug.cgi?id=1138137" rel="nofollow noreferrer">https://bugzilla.redhat.com/show_bug.cgi?id=1138137</a></p>
  
  <p>I am also behind Cloudflare, which may be part of the issue.</p>