node.js write http response to stream


Question

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

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

now the output file is created but the filesize is 0. The url is correct though, i verified that with wget.

Thanks in advance & best regards

1
19
8/10/2012 5:02:58 PM

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.

22
8/10/2012 6:57:36 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon