我正在考虑将这个文本文件从互联网加载到我的nodejs服务器(到内存中):
http://data.iana.org/TLD/tlds-alpha-by-domain.txt
我想提取每一行并将其放入一个我可以稍后搜索的数组中,但我无法让它工作。
我尝试过使用request,fs和readline:
var request = require('request');
var lineCnt = 0;
var result = [];
request.get('http://data.iana.org/TLD/tlds-alpha-by-domain.txt', function (error, response, body) {
if (!error && response.statusCode == 200) {
var csv = body;
var fs = require('fs');
var readline = require('readline');
readline.createInterface({
input: fs.createReadStream(body),
terminal: false
}).on('line', function(line) {
if(lineCnt == 0){ //do nothing skip the first line}
else{
result.push(line);
}
lineCnt = lineCnt + 1; //increment
});
}
});
虽然数组为空,但它不起作用。我收到以下错误:
Error: ENAMETOOLONG: name too long, open '# Version 2017041700, Last Updated Mon Apr 17 07:07:01 2017 UTC ... [the rest of the file]'
答案 0 :(得分:0)
你甚至不需要像request
这样的模块。但是,如果你要使用request
,那么你应该做的就是不使用"缓冲的回调"功能但获取响应流并将其传递给readline.createInterface()
。例如:
request.get('http://data.iana.org/TLD/tlds-alpha-by-domain.txt')
.on('response', (response) => {
if (response.statusCode === 200) {
var readline = require('readline');
readline.createInterface({
input: response,
terminal: false
}).on('line', function(line) {
if(lineCnt == 0){ //do nothing skip the first line}
else{
result.push(line);
}
lineCnt = lineCnt + 1; //increment
});
}
}).on('error', ...);