验证来自grunt任务的http响应

Verify http response from grunt task

本文关键字:http 响应 任务 grunt 验证      更新时间:2023-09-26

在将它们添加到javascript文件中之前,我需要验证从web服务获取的4000多个URL的有效性。我正在使用一个grunt任务对这些url进行一些清理操作,在将它们添加到js文件之前,我还想验证每个url是否返回了200个HTTP状态代码,所以在grunt任务中。

在本例中,根据validate_url任务的结果,我需要修改urlToProxy数组

为了清晰起见,我想要构建的整个任务是:

  1. 从API读取URL并写入文件
  2. 清理url列表(另一项任务,不在示例代码中)
  3. 验证url以检查服务器是否以200响应
  4. 将url写入包含简单js数组的文件

关于我该怎么做有什么想法/建议吗?

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){
            // YES 200 returned
          } else {
            // OOPS NO 200 returned
          }
        }
      }
    }
});
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]);
    }
}

proxyUrl.js内容

{ "record": "audentes.com'nfortuna.com'niuvat.com'n...'nwww.google.com" }

您可以在Grunt中使用curl和exec来实现这一点。

下面是一个完整的代码示例。别忘了创建一个package.json。运行它应该是这样的:

$npm init
$npm install grunt-exec --save
$grunt

这应该在您的Gruntfile.js

module
    .exports = function(grunt) {
        grunt
            .loadNpmTasks('grunt-exec'); // register the exec task (from package.json)
        grunt
            .initConfig({
                exec: {
                  validate_url: {
                    cmd: function(url){
                        console
                            .log('validate: ' + url); // log that we are validating
                        return 'curl -sL -w "%{http_code}|%{url_effective}''n" "http://www.google.com" -o /dev/null -m 5'; // the actual curl. Note the -m 5, this is a 5 second timeout. Also note the -w, this is the writeout which we will split later
                    },
                    callback: function (error, stdout, stderr) {
                        var stdoutSplit = stdout
                                            .split('|');
                        if(stdoutSplit[0]==200){ // 200 status is returned
                            console
                                .log('YES 200, the requested url was: ' + stdoutSplit[1]); // YES 200 returned
                        } else {
                            console
                                .log('Crap, no 200, the requested url was: ' + stdoutSplit[1]);// OOPS NO 200 returned
                        }
                    }
                  }
                }
            });
        grunt
            .registerTask('default', function() {
                grunt
                    .file
                    .readJSON('proxyUrls.json')
                    .record.split(''n')
                    .forEach(function(val){
                        if(val !== ''){
                            grunt.task.run('exec:validate_url:' + val);
                        }
                    });
            });
    };

注意
从您自己的命令行尝试:curl -sL -w "%{http_code}''n" "https://pastebin.com/raw.php?i=RPSbdNgR" -o /dev/null