node.js write http response to stream

前端 未结 3 1537
南笙
南笙 2020-12-03 05:21

i\'m fetching some binary data over http. My code looks like:

var writeStream = fs.createWriteStream(fileName);
request(url, function(err, res) {
    res.soc         


        
相关标签:
3条回答
  • 2020-12-03 05:43

    The callback for http.request only supplies one argument, which is a reference to the response of the request. Try

    http.request(url, function(res) {
        res.pipe(writeStream);
    });
    

    Also note that the ClientResponse implements ReadableStream, so you should use .pipe rather than .socket.pipe.

    0 讨论(0)
  • 2020-12-03 05:49

    I'm assuming that here request is from mikeal's request library rather than being an instance of http.request. In that case you can simply do request(url).pipe(writeStream);

    Remember that for debugging purposes, you can always pipe to process.stdout.

    0 讨论(0)
  • 2020-12-03 05:55

    var readStream = fs.createReadStream(fileName);
    request(url, function(err, res) {
      readStream.pipe(res);
      readStream.on('end', function() {
        //res.end({"status":"Completed"});
      });
    });

    0 讨论(0)
提交回复
热议问题