NodeJS将csv文件下载到缓冲区

时间:2015-05-12 10:34:07

标签: javascript node.js csv

我想下载一个小的csv,我需要在进程之前将它存储在一个变量中:

var http = require('http');

var csvData;
var request = http.get('http://url', function(response) {
  response.pipe(csvData);
});
request.end();

response.pipe()仅适用于文件流,如何将我的回复存储到csvData var?

1 个答案:

答案 0 :(得分:2)

只需为dataend事件创建监听器:

var http = require('http');

var csvData = '';
var request = http.get('http://url', function(response) {
    response.on('data', function(chunk) {
        csvData += chunk;
    });
    response.on('end', function() {
        // prints the full CSV file
        console.log(csvData);
    });
});