Created
August 31, 2012 20:09
-
-
Save iwek/3558258 to your computer and use it in GitHub Desktop.
Incorrect example of curl with nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var request = require('request'); | |
var urls = new Array("http://www.yahoo.com","http://www.bing.com"); | |
for (var i = 0; i < urls.length; i++) { | |
var file = 'log'+[i]+'.txt'; | |
var url = urls[i]; | |
console.log(url); | |
console.log(file); | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log('request url: '+url); | |
console.log('request file: '+file); | |
fs.writeFile(file, body); | |
} | |
}); | |
} | |
/* | |
Output: | |
http://www.yahoo.com | |
log0.txt | |
http://www.bing.com | |
log1.txt | |
request url: http://www.bing.com | |
request file: log1.txt | |
request url: http://www.bing.com | |
request file: log1.txt | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment