Matthew John Miernik
Matthew John Miernik

Reputation: 21

How to read file by url in node js

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?

Upvotes: 0

Views: 5725

Answers (1)

Manuel Spigolon
Manuel Spigolon

Reputation: 12900

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();

Upvotes: 1

Related Questions