I need to check the accuracy of 4000+ URLs I get from a web service before adding them to a javascript file. I use the grunt task to perform some cleanup operation on these URLs, and I would also like to check that each URL returns 200 HTTP status code before adding them to the js file, therefore as part of the grunt task.
In an example based on the result of the valiate_url task, I need to change the urlToProxy array
For clarity, the whole task I want to build is:
- Read URLs from API and write to file
- Clear the URL list (another task, not sample code)
- Validate URLs to check if server responds with 200
- Write the url of the file containing a simple js array
Any idea / suggestion on how I can do this?
grunt.initConfig({
...
exec: {
validate_url: {
cmd: function(url){
return 'curl -sL -w "%{http_code}\\n" "http://' + url + '" -o /dev/null';
},
callback: function (error, stdout, stderr) {
if(stdout==200){
} else {
}
}
}
}
});
grunt.registerTask('readconfig', 'reads the configuration', function() {
var urls = grunt.file.readJSON('.tmp/proxyUrls.json');
var urlsToProxy = urls.record.split('\n');
for(var i = urlsToProxy.length - 1; i >= 0; i--) {
grunt.task.run('exec:validate_url:' + 'urlsToProxy[i]);
}
}
content proxyUrl.js
{ "record": "audentes.com\nfortuna.com\niuvat.com\n...\nwww.google.com" }
source
share