Read remote file with node.js (http.get)

后端 未结 4 1533
野趣味
野趣味 2020-11-29 05:26

Whats the best way to read a remote file? I want to get the whole file (not chunks).

I started with the following example

var get = http.get(options)         


        
4条回答
  •  囚心锁ツ
    2020-11-29 06:22

    function(url,callback){
        request(url).on('data',(data) => {
            try{
                var json = JSON.parse(data);    
            }
            catch(error){
                callback("");
            }
            callback(json);
        })
    }
    

    You can also use this. This is to async flow. The error comes when the response is not a JSON. Also in 404 status code .

提交回复
热议问题