您可以使用流存档。
您选择的库line-by-line
支持它们,因此您只需这样做:
- 创建一个可读流(在文件系统中或通过 http 从在线资源中形成文件)
- 将流传递到您的库并监听事件
这有效,请注意您需要http
或https
基于您的 url,我的示例是 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();