I am using nodejs and expressjs. I wonder if there is something like request.headers.protocol in the clientRequest object. I would like to build the baseUrl for the web links. So if the request was done via https I would like to keep https in all links.
var baseUrl = request.headers.protocol + request.headers.host;
Edit: For Express, it’s safer and recommended to use
req.secure(as @Andy recommends below). While it uses a similar implementation, it will be safe for future use and it also optionally supports theX-Forwarded-Protoheader.That being said, for your use case it would be quicker to use Express’
req.protocolproperty, which is eitherhttporhttps. Note, however, that for outgoing links, you can just refer to//example.com/path, and the browser will use the current protocol. (See also Can I change all my http:// links to just //?)For node
Requestobject without Express:It’s inreq.connection.secure(boolean).Edit:
The API has changed, for Node 0.6.15+:
An HTTPS connection has
req.connection.encrypted(an object with information about the SSL connection). An HTTP connection doesn’t havereq.connection.encrypted.Also (from the docs):