Assuming I have an array of URLs and I want to ensure each URL is working I have created the following code. However only the last URL in the array is getting tested. How can I ensure each url returns a 200 response code? To be clear these are all remote addresses I am testing and they point to decently sized PDFs.
Updated based on @lukas.pukenis's response. The results are similar, only a few files are actually checked.
function check(l) {
console.log(l);
http.get(l, function(res) {
if (res.statusCode != 200) {
console.log(res.statusCode + ' on '+l);
} else {
console.log('success on ' + l);
}
});
}
for (link in fileLinks) {
check(fileLinks[link]);
}
This code outputs:
http://somesite.com/somefile1.pdf
http://somesite.com/somefile2.pdf
http://somesite.com/somefile3.pdf
...
all the rest of them
...
http://somesite.com/somefile99.pdf
success on http://somesite.com/somefile1.pdf
success on http://somesite.com/somefile2.pdf
404 on http://somesite.com/somefile5.pdf
success on http://somesite.com/somefile7.pdf
fileLinksis an Array, try using the.forEach(function(item){})function instead offor, which is used to loop through the various keys in a javascript object. It's not meant to be used with arrays.