Skip to content

Instantly share code, notes, and snippets.

@huzemin
Created January 22, 2016 04:08
Show Gist options
  • Save huzemin/a05b1cae32099967b296 to your computer and use it in GitHub Desktop.
Save huzemin/a05b1cae32099967b296 to your computer and use it in GitHub Desktop.
下载网络文件
var http = require('http');
var url = require('url');
var debug = require('debug')('download');
var fs = require('fs');
var path = require('path');
debug('booting %s', "download.js")
var url_parms = url.parse("http://img2.chouti.com/CHOUTI_8CEB537A0B9E46BDA7B744ABED8C3812_W163H163=C200x200.jpg");
var options = {
protocol: url_parms.protocol,
hostname: url_parms.hostname,
method: 'GET',
path: url_parms.pathname,
}
var req = http.request(options, function(res) {
if(res.statusCode) {
var data = '';
var stream = fs.createWriteStream(path.basename(url_parms.pathname), { flags: 'w+',});
res.pipe(stream);
res.on('end', function(){
debug('-----Finished----');
});
}else {
debug('Fail Http statusCode: %s', res.statusCode);
}
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment