1var http = require('http');
2var fs = require('fs');
3
4var download = function(url, dest, cb) {
5 var file = fs.createWriteStream(dest);
6 var request = http.get(url, function(response) {
7 response.pipe(file);
8 file.on('finish', function() {
9 file.close(cb); // close() is async, call cb after close completes.
10 });
11 }).on('error', function(err) { // Handle errors
12 fs.unlink(dest); // Delete the file async. (But we don't check the result)
13 if (cb) cb(err.message);
14 });
15};