Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created February 7, 2011 04:07
Show Gist options
  • Save indexzero/813983 to your computer and use it in GitHub Desktop.
Save indexzero/813983 to your computer and use it in GitHub Desktop.
A simple download function using request
//
// Simple download using 'request'
//
function download (localFile, remotePath, callback) {
var localStream = fs.createWriteStream(localFile);
request({ uri: remotePath, responseBodyStream: localStream }, function (err, response, stream) {
if (err) return callback(err);
else if (response.statusCode < 200 || response.statusCode >= 300) {
callback(new Error('Invalid HTTP response code: ' + response.statusCode));
return;
}
stream.on('close', function () {
callback(null, localFile, localStream);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment