I want to down some image on the site ,the image are saved by
http://a.com/1.jpg
http://a.com/2.jpg
http://a.com/3.jpg
So, I write the code to do it
My code is:
var request = require('request');
var fs = require('fs');
var mkdir = require('mkdirp');
var preUrl = 'http://a.com/';
var dir = './images';
mkdir(dir, function(err) {
if(err) {
console.log(err);
}
});
for(var i = 1; i <= 10; i++){
var trueUrl = preUrl + i + '.jpg';
var name = i + '.jpg';
request(trueUrl, function(err, response, body) {
if(!err && response.statusCode == 200) {
download(trueUrl, dir, name);
console.log("Done" + trueUrl);
}
});
}
var download = function(url, dir, filename) {
request.head(url, function(err, res, body) {
request(url).pipe(fs.createWriteStream(dir + '/' + filename));
});
};
but there is some error in the code.
the code cant't run the
request(trueUrl, function(err, response, body) {
if(!err && response.statusCode == 200) {
download(trueUrl, dir, name);
console.log("Done" + trueUrl);
}
});
So,What should I do?
Now, I found the console will output the done http://a.com/10.jpg.
and last, the download image is only 10.jpg
Use
url.resolve
when joining url elements to prevent errors like this. (https://nodejs.org/api/url.html#url_url_resolve_from_to)