Skip to content

Instantly share code, notes, and snippets.

@jackfranklin
Last active December 21, 2015 01:08
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 jackfranklin/6225121 to your computer and use it in GitHub Desktop.
Save jackfranklin/6225121 to your computer and use it in GitHub Desktop.
// I have this code for downloading a URL to a file
var stream = request(url).pipe(fs.createWriteStream(fileDestination))
// I can bind to "close" for when the file is completely downloaded, which works fine
stream.on("close", function() {...});
// is there an event I can bind to that will be fired whenever data is receieved (so not that the file is 100% done, but that a bit more of the file has been downloaded?
stream.on("???", function() {...});
// use case: showing a progress bar of downloading the file
// I was lead to believe I wanted the "data" event, but this doesn't work and I never see the message:
stream.on("data", function() { process.stdout.write("hey"); }); //no work :(
@jackfranklin
Copy link
Author

@PhUU got i!

the process of piping the stream swallows up all events. So you have to save the stream before piping:

var stream = request(url);
stream.pipe(fs.createWriteStream(fileDestination));
stream.on("data", function(chunk) { ... });
stream.on("close", function() {...});

Works like a dream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment