在继续下一次迭代之前,NodeJS用管道完成了文件的写入

时间:2018-11-08 19:27:33

标签: javascript node.js synchronization pipe

类似于此question

我有一个脚本,可以通过http.get将文件下载到给定的URL。

在仅使用pipe模块继续下一次迭代之前,如何确保http/https已完成?

    //nodejs default libs
    var fs = require("fs"); 
    var http = require('https');

    function dlFile(fullFilePath, dlUrl, fsize, fname){
        var file = fs.createWriteStream(fullFilePath); //fullFilePath will dictate where we will save the file + filename.
        var rsult ='';
        var downloadedFsize;
        var stats; //stats of the file will be included here

        var request = http.get( dlUrl, function(response) {
                let rsult = response.statusCode;
                //will respond with a 200 if the file is present
                //404 if file is missing 
                response.pipe(file);

                /*pipe writes the file... 
                  how do we stop the iteration while it is not yet finished writing?
                */

                console.log(" \n FILE  : " + fname);
                console.log("File analysis finished : statusCode: " +  rsult + " || Saved on " +  fullFilePath);
                console.log(' \n Downloaded from :' + dlUrl);
                console.log(' \n SQL File size is : ' + fsize);
                //identify filesize 
                stats = fs.statSync(fullFilePath);
                downloadedFsize = stats["size"]; //0 because the pipe isn't finished yet...

                console.log(' actual file size is : ' + downloadedFsize);
            }).on('error', function(e) {
                console.error(e);
                //log that an error happened to the file
            }).on('end', function(e){
                //tried putting the above script here but nothing happens
            });
        return rsult;   
}

是否有一种更清洁的方法,类似于我上面所想到的?还是我应该采取不同的方式?我尝试将代码放到.on('end'上,但是它什么也没做

2 个答案:

答案 0 :(得分:1)

end事件不是在请求上触发,而是在响应(docs)上触发:

 response.on("end", function() {
   console.log("done");
 });

答案 1 :(得分:0)

正如@Jonas Wilms所说,触发确实是在响应中。

//nodejs default libs
    var fs = require("fs"); 
    var http = require('https');

    function dlFile(fullFilePath, dlUrl, fsize, fname){
        var file = fs.createWriteStream(fullFilePath); //fullFilePath will dictate where we will save the file + filename.
        var rsult ='';
        var downloadedFsize;
        var stats; //stats of the file will be included here

        var request = http.get( dlUrl, function(response) {
                let rsult = response.statusCode;
                //will respond with a 200 if the file is present
                //404 if file is missing 
                response.pipe(file).on('finish', function(e){
                   console.log(" \n FILE  : " + fname);
                   console.log("File analysis finished : statusCode: " +  rsult + " || Saved on " +  fullFilePath);
                   console.log(' \n Downloaded from :' + dlUrl);
                   console.log(' \n SQL File size is : ' + fsize);
                   //identify filesize 
                   stats = fs.statSync(fullFilePath);
                   downloadedFsize = stats["size"]; 
                   console.log(' actual file size is : ' + downloadedFsize);
                });

                /*pipe writes the file above, and output the results once it's done */


            }).on('error', function(e) {
                console.error(e);
                //log that an error happened to the file
            }).on('end', function(e){
                //tried putting the above script here but nothing happens
            });
        return rsult;   
}