Monday, 17 April 2017

Get file from internet, load into memory and take each line into an array

I am looking at getting this text file from the internet loaded into my nodejs server (into memory):

http://data.iana.org/TLD/tlds-alpha-by-domain.txt

I want to extract each line and place it into an array which I can search later, but I have not been able to get it to work.

I have tried using request, fs, and 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
       });
    }
});

It does not work though as the array is empty. I get the following error:

Error: ENAMETOOLONG: name too long, open '# Version 2017041700, Last Updated Mon Apr 17 07:07:01 2017 UTC ... [the rest of the file]'



via user2924127

No comments:

Post a Comment