Asynchronously reading and caching multiple files in nodejs

后端 未结 2 1454
清酒与你
清酒与你 2020-12-15 02:11

I have an array which keeps URL of several files. For example:

var files = [\'1.html\', \'2.html\', \'3.html\'];

I need to read them asynch

2条回答
  •  囚心锁ツ
    2020-12-15 02:20

    The existing answer didn't work for me. I did find an NPM package which did the job: https://www.npmjs.com/package/read-multiple-files. After npm install read-multiple-files at the command line, here's the code I used:

    var files = ['1.html', '2.html', '3.html'];
    
    console.log("\n");
    
    readMultipleFiles(files, 'utf8', function(err, inputFiles) {
      if(err) {
        console.log("Read Error: " + err);
      }
    
      fileOne = inputFiles[0];
      fileTwo = inputFiles[1];
      ...
    
      console.log(fileOne);
      console.log(fileTwo);
    
    });
    

提交回复
热议问题