title: Determine request protocol in native Node.js
body: <p>How would I get the request protocol (<code>https</code>, <code>https</code> or perhaps something else) in native Node.js?</p>
  
  <p>I found alot of documentation for this for the Express framework, not nothing for native Node, which is what Im using. </p>
  
  <p>Suppose I have a basic server:</p>
  
  <pre><code>var app = http.createServer(function(req, res){
  
      var url = req.url;
      console.log(url);
  
      var headers = req.headers;
      console.log(headers);
  
  }).listen(8000);
  </code></pre>
  
  <p>..and I make a request to <code>localhost:8000</code></p>
  
  <p>I get the log output:</p>
  
  <pre><code>/   &lt;--- the request url
  
  {   
    host: 'localhost:8000',
    connection: 'keep-alive',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
     accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9' 
  }
  </code></pre>
  
  <p><strong>but where is the protocol?</strong> </p>
  
  <p>I understand that Im running the server locally, but the protocol should still be <code>http</code>. </p>
  
  <p>EDIT:</p>
  
  <p>For context, I am trying to set up an http to https redirect. </p>
  
  <p>If someone requests <code>https://example.com/foo</code> I would redirect them to <code>https://example.com/foo</code>.</p>