Stop downloading the data in nodejs request

后端 未结 2 812
名媛妹妹
名媛妹妹 2021-01-05 13:39

How can we stop the remaining response from a server - For eg.

http.get(requestOptions, function(response){

//Log the file size;
console.log(\'File Size:\'         


        
2条回答
  •  迷失自我
    2021-01-05 13:59

    You need to perform a HEAD request instead of a get

    Taken from this answer

    var http = require('http');
    var options = {
        method: 'HEAD', 
        host: 'stackoverflow.com', 
        port: 80, 
        path: '/'
    };
    var req = http.request(options, function(res) {
        console.log(JSON.stringify(res.headers));
        var fileSize = res.headers['content-length']
        console.log(fileSize)
      }
    );
    req.end();
    

提交回复
热议问题