0

Similar to this question,

I have a script that downloads a file to a given url via http.get.

How can I make sure the pipe is finished before continuing to the next iteration with just the http/https module??

    //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;   
}

Is there a cleaner approach similar to what I have in mind above? or should I approach this differently? I tried putting the code on .on('end' but it does nothing

Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40

2 Answers2

1

The end event is not triggered on the request, instead it is triggered on the response (docs):

 response.on("end", function() {
   console.log("done");
 });
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I used `finish` instead since the file was apparently `writable` (even though it's actually a blob), but otherwise this is good, thanks – Malcolm Salvador Nov 08 '18 at 20:22
0

As @Jonas Wilms says, the trigger was indeed on response.

//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;   
}
Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40