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