download-to-file.js 583 B

123456789101112131415161718
  1. var fs = require('fs'),
  2. needle = require('./..'),
  3. path = require('path');
  4. var url = process.argv[2] || 'https://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png';
  5. var file = path.basename(url);
  6. console.log('Downloading ' + file);
  7. needle.get(url, { output: file, follow: 3 }, function(err, resp, data){
  8. console.log('File saved: ' + process.cwd() + '/' + file);
  9. var size = fs.statSync(file).size;
  10. if (size == resp.bytes)
  11. console.log(resp.bytes + ' bytes written to file.');
  12. else
  13. throw new Error('File size mismatch: ' + size + ' != ' + resp.bytes);
  14. });