title: node.js webtorrent collect all files by magnet link
body: <p>I have not used node.js before.<br>
  Have a .txt file with list of magnet links.
  Want to write a json file with list of all files contained in these links. </p>
  
  <pre><code>var WebTorrent = require('webtorrent');
  var fs = require('fs');
  var client = new WebTorrent();
  var array = fs.readFileSync('yop.txt').toString().split("\n");
  i = 0;
  while (i &lt; array.length) {
  //console.log(array[i]);
  var magnetURI = array[i];
  n = 0;
  client.add(magnetURI, function (torrent) {
      torrent.files.forEach(function (file) {
          //console.log( file.name)
          jsonString = JSON.stringify({'book': file.name});
          fs.appendFile("data.json", jsonString, function (err) {
              if (err) {console.log(err);} else { n++ }
          });
          if (n == torrent.files.length) {i++ }
      })
  })
  </code></pre>
  
  <p>}</p>
  
  <p><a href="http://i.stack.imgur.com/hY6Qr.png" rel="nofollow">when run gives the following error</a>
  <br>
  Sorry for such a terrible code.</p>