0

I using line-by-line module for node js, and I want to set file from url for example now i have this:

LineByLineReader     = require('line-by-line'),
  lr                   = new LineByLineReader('file.txt');

And i want to

LineByLineReader     = require('line-by-line'),
  lr                   = new LineByLineReader('http://test.pl/file.txt');

Is it possible?

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73

1 Answers1

1

you can archive that using streams.

The lib you have choosen line-by-line support them so you have only to do like so:

  • create a readable stream (form a file in the filesystem or from a resource online via http)
  • pass the stream to your lib and listen for events

This works, note that you need to require http or https based on your url, my example is https

const http = require('https');
const LineByLineReader = require('line-by-line')

const options = {
  host: 'stackoverflow.com',
  path: '/questions/54251676/how-to-read-file-by-url-in-node-js',
  method: 'GET',
};

const req = http.request(options, (res) => {
  res.setEncoding('utf8');

  lr = new LineByLineReader(res);


  lr.on('error', function (err) {
    console.log('err', err);
  });

  lr.on('line', function (line) {
    console.log('line', line);
  });

  lr.on('end', function () {
    console.log('end');
  });

});
req.on('error', (e) => {
  console.log('problem with request', e);
  req.abort();
});
req.end();
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73