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
.