node.jsstreamline-by-line

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?


Solution

  • you can archive that using streams.

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

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