HTTP请求获取视频还返回200状态代码和Parse错误也在Node JS中?

时间:2018-04-27 11:20:33

标签: javascript node.js http express request

我正在尝试从某个URL下载视频,它正在下载视频,但也会返回Parse Error,因此回调会发生两次。

http.get(url, function(response) {
        console.log("url "+url)
        console.log("Got response: " + response.statusCode);
        if(response && response.statusCode == 200){//OK
            mkdirp(dest,function(err){ 
                if(err){
                      console.log("err "+err);
                }else{ //create path dynamically
                    console.log("else SUCCESS")
                    var file = fs.createWriteStream("/mydrive/test.flv");
                    response.pipe(file);
                    file.on('finish', function() {
                        //console.log("finish file creation....")
                      file.close();
                    });
                } 
             });  
        }else{
           console.log("No video "+response.statusCode);
        }         
 }).on('error', function(e) {
    console.log("Got error: " + e.message);
 });

输出我得到的是回调发生两次。

我想要回复或失败回调:

Got response: 200

Got error: Parse Error

0 个答案:

没有答案