我将逐行模块用于节点js,现在想从URL设置文件,例如:
LineByLineReader = require('line-by-line'),
lr = new LineByLineReader('file.txt');
我想
LineByLineReader = require('line-by-line'),
lr = new LineByLineReader('http://test.pl/file.txt');
有可能吗?
答案 0 :(得分:1)
您可以使用流将其存档。
您选择的line-by-line
库支持它们,因此您只需要这样做:
这可行,请注意,您需要根据您的网址要求http
或https
,我的例子是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();