I'm writing a downloader with node.js and the request module. Using the stream syntax I'm doing
var r = request(url).pipe(fs.createWriteStream(targetPath));
r.on('error', function(err) { console.log(err); });
r.on('finish', cb);
to download the file, save it and call the callback. However, in almost 50% of the cases the file is either empty or not created at all. No error
event is emitted. It seems like the finish
event is triggered even though the file wasn't (completely) written yet.
Context: The whole thing is wrapped into async.each
calls.
Any clues? Thanks!
You need to close the file before accessing it:
Incidentally, if the url replies with any http error (such as a 404 not found), that won't trigger the 'error' event, so you should probably check that separately: