NodeJS loads csv file into buffer

I am linking to download a small csv and I need to store it in a variable before the process:

var http = require('http');

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

      

response.pipe()

only works with file stream, how can I store the response to csvData

var?

+3


source to share


1 answer


Just create an event listener data

and 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);
    });
});

      

+3


source







All Articles