2

How would I get the request protocol (https, https or perhaps something else) in native Node.js?

I found alot of documentation for this for the Express framework, not nothing for native Node, which is what Im using.

Suppose I have a basic server:

var app = http.createServer(function(req, res){

    var url = req.url;
    console.log(url);

    var headers = req.headers;
    console.log(headers);

}).listen(8000);

..and I make a request to localhost:8000

I get the log output:

/   <--- 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' 
}

but where is the protocol?

I understand that Im running the server locally, but the protocol should still be http.

EDIT:

For context, I am trying to set up an http to https redirect.

If someone requests https://example.com/foo I would redirect them to https://example.com/foo.

yevg
  • 1,846
  • 9
  • 34
  • 70
  • 2
    What problem are you really trying to solve because in your example, you know exactly which protocol your server was started with (it's `http.createServer()`).. This isn't something you need to look in the `req` object to get. You can't get an `https` request from an `http` server's request handler. If you're trying to have both an http server and an https server and use the same request handler, then that's an easy problem to solve, but you have to tell us what problem you're really trying to solve? – jfriend00 Feb 14 '18 at 00:23
  • I am trying to set up an http -> https redirect. I have edited the question above to provide context. thanks! – yevg Feb 14 '18 at 02:48
  • 1
    You just put the redirect logic into the http server request handler where you already `know` it's http, not https. Simple http redirect to https server here: https://stackoverflow.com/a/23977269/816620. – jfriend00 Feb 14 '18 at 02:55
  • oh! so an https request would not be "caught" by http - that makes sense. https requests would be processed by the https handler. correct? – yevg Feb 14 '18 at 02:59
  • 1
    Yes. Your http and https servers need to run on different ports and be different servers (they can both be in the same node.js process). The default port for http is 80 and for https is 443. That's what the browser uses if there is no port specified in the URL. – jfriend00 Feb 14 '18 at 03:01
  • 1
    Plus an http request coming into the https server would fail to connect. And, an https request coming into an http server would also fail to connect because an https client reuiqres an https server and an https server requires an https client. – jfriend00 Feb 14 '18 at 03:05
  • but wouldnt that necessitate writing duplicate code? unless you place all your logic in callbacks, I guess. also I have a live app on heroku with a managed certificate who's node server only has the http handler. yet https works on that app – yevg Feb 14 '18 at 03:05
  • 1
    If the only point of the http server is to redirect to https, that takes a few lines of code and that server does nothing but redirect. – jfriend00 Feb 14 '18 at 03:07
  • 1
    Heroku may be managing the https for you with a proxy and sending plain http to your actual server process. If so, that proxy probably has a feature to do the https redirect for you. – jfriend00 Feb 14 '18 at 03:08
  • ok got it. thank you for your help! – yevg Feb 14 '18 at 03:14
  • See: [Https Redirect for Node on Heroku](https://jaketrent.com/post/https-redirect-node-heroku/). That says that on Heroku, you can use the `x-forwarded-proto` header to see the original URL, including the http or https. – jfriend00 Feb 14 '18 at 03:14

0 Answers0