我想下载一个小的csv,我需要在进程之前将它存储在一个变量中:
var http = require('http');
var csvData;
var request = http.get('http://url', function(response) {
response.pipe(csvData);
});
request.end();
response.pipe()
仅适用于文件流,如何将我的回复存储到csvData
var?
答案 0 :(得分:2)
只需为data
和end
事件创建监听器:
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);
});
});