Skip to content

Instantly share code, notes, and snippets.

@tommedema
Created February 5, 2011 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommedema/812484 to your computer and use it in GitHub Desktop.
Save tommedema/812484 to your computer and use it in GitHub Desktop.
//fired when request has been answered
httpRequest.on('response', function (response) {
//set encoding
response.setEncoding('utf8');
//setup output stream
var filePath = 'someFile.csv';
console.log('Now writing: ' + filePath);
var writeStream = fs.createWriteStream(filePath, {
'flags': 'w+',
'encoding': 'utf8',
'mode': 0666
});
writeStream.on('error', function(error) {
sys.debug('Error while writing: ' + error);
process.exit();
});
//fired on chunk data receival
response.on('data', function (chunk) {
//write to output stream
writeStream.write(chunk);
});
//fired when no more data will be received
response.on('end', function() {
console.log("Download was successful, saved to file. Checking queue again soon.");
//close output stream when ready
writeStream.on('drain', function() {
writeStream.removeAllListeners('drain');
writeStream.end(); //THIS CAUSES BAD FILE DESCRIPTOR ERROR
writeStream.destroy(); //THIS CAUSES BAD FILE DESCRIPTOR ERROR
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment