使用node.js(http.get)读取远程文件

什么是读取远程文件的最佳方式? 我想获得整个文件(不是块)。

我从下面的例子开始

var get = http.get(options).on('response', function (response) { response.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); 

我想parsing文件为csv,但为此我需要整个文件,而不是分块的数据。

我会用这个请求 :

 request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png')) 

或者,如果您不需要先将文件保存到文件中,并且只需将CSV读入内存,则可以执行以下操作:

 var request = require('request'); request.get('http://www.whatever.com/my.csv', function (error, response, body) { if (!error && response.statusCode == 200) { var csv = body; // Continue with your processing here. } }); 

等等

 http.get(options).on('response', function (response) { var body = ''; var i = 0; response.on('data', function (chunk) { i++; body += chunk; console.log('BODY Part: ' + i); }); response.on('end', function () { console.log(body); console.log('Finished'); }); }); 

对此的更改,这是可行的。 任何意见?