NodeJS finish writing the file with pipe before continuing with the next iteration











up vote
0
down vote

favorite












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










share|improve this question




























    up vote
    0
    down vote

    favorite












    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










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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










      share|improve this question















      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







      javascript node.js synchronization pipe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 19:33

























      asked Nov 8 at 19:27









      Malky.Kid

      1,0961327




      1,0961327
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










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



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





          share|improve this answer





















          • I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
            – Malky.Kid
            Nov 8 at 20:22


















          up vote
          0
          down vote













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





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214832%2fnodejs-finish-writing-the-file-with-pipe-before-continuing-with-the-next-iterati%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










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



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





            share|improve this answer





















            • I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
              – Malky.Kid
              Nov 8 at 20:22















            up vote
            1
            down vote



            accepted










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



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





            share|improve this answer





















            • I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
              – Malky.Kid
              Nov 8 at 20:22













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






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



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





            share|improve this answer












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



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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 19:39









            Jonas Wilms

            52.3k42445




            52.3k42445












            • I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
              – Malky.Kid
              Nov 8 at 20:22


















            • I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
              – Malky.Kid
              Nov 8 at 20:22
















            I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
            – Malky.Kid
            Nov 8 at 20:22




            I used finish instead since the file was apparently writable (even though it's actually a blob), but otherwise this is good, thanks
            – Malky.Kid
            Nov 8 at 20:22












            up vote
            0
            down vote













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





            share|improve this answer

























              up vote
              0
              down vote













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





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









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





                share|improve this answer












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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 20:22









                Malky.Kid

                1,0961327




                1,0961327






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214832%2fnodejs-finish-writing-the-file-with-pipe-before-continuing-with-the-next-iterati%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff