2

I have 2 servers and I would like to get a file from one with a GET request and send it to the other server with a POST request.

The easiest solution is to store the file from the first server and then (once the download is finished) to send it to the other.

But, instead of doing it in 2 time, I'm looking for doing it in one time. I try to stream directly the data from the get to the post.

I have the following code :

var stream = function(getOptions, postOptions, dest, cb) {
var fileRead = fs.createWriteStream(dest);

var getRequest = https
    .request(getOptions, function(response) {
        response.pipe(fileRead);
        fileRead.on('finish', function() {
            fileRead.close(cb);
        });
    })
 //  .on('error', function(err) { 
    //     fs.unlink(dest); 
    // })
    .end();

var fileWrite = fs.createReadStream(dest).pipe(https.request(postOptions).end());

}

The file is get correctly but I do not know how to send the data directly to the second server. I tried the code above, it's not working (dest in not a function).

References: Sending large image data over HTTP in Node.js

Thank you!

Community
  • 1
  • 1
acorbel
  • 1,300
  • 12
  • 14

0 Answers0