【发布时间】:2016-09-11 17:37:43
【问题描述】:
我之前没用过node.js。
有一个包含磁铁链接列表的 .txt 文件。
想要编写一个包含这些链接中包含的所有文件列表的 json 文件。
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 < 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++ }
})
})
}
when run gives the following error
对不起这么糟糕的代码。
【问题讨论】:
标签: javascript node.js webtorrent