1

I need to be able to retrieve a PDF document from a URL and pass it to another function which will eventually store it in a database. I have found many solutions for DOWNLOADING files to local storage but I do not want to do this. The simplest method to grab the file that I found looks like this:

var http = require('http');
var fs = require('fs');

var download = function(url, dest, cb) {
  var file = fs.createWriteStream(dest);
  var request = http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);
    });
  });
}  

How do I take the information from the request and stick it into a constant so i can then use it in another function?

Thanks

David McIntyre
  • 218
  • 1
  • 9

1 Answers1

1

If you use response.pipe(file) you are writing to a writable stream, in this case a file. As you don't want to use local storage, you should choose a different approach.

You could read the raw data chunks and combine them into a Buffer object as described here: https://stackoverflow.com/a/21024737/7821823. You can then pass the buffer to another function.

http.get(url), function(res) {
    var data = [];

    res.on('data', function(chunk) {
        data.push(chunk);
    }).on('end', function() {
        //at this point data is an array of Buffers
        //so Buffer.concat() can make us a new Buffer
        //of all of them together
        var buffer = Buffer.concat(data);
        console.log(buffer.toString('base64'));
    });
});
Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33