How to know if a request is http or https in node.js

前端 未结 9 1257
别跟我提以往
别跟我提以往 2020-12-08 04:11

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

9条回答
  •  执念已碎
    2020-12-08 04:25

    If you are using request module and for example want to know what protocol does some www use, you can use: response.request.uri.protocol

    request(YOUR_TARGET, function(error, response, body){
        if (error){
            console.log(error);
        }
        else {
            console.log(response.request.uri.protocol); // will show HTTP or HTTPS
        }
    });
    

    If you need user protocol then use request.headers.referer.split(':')[0]; just like @Harsh gave you.

提交回复
热议问题