验证来自 grunt 任务的 http 响应

Verify http response from grunt task

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

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

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

  1. 从 API 读取 urls 并写入文件
  2. 清理 url 列表(另一个任务,不在示例代码中)
  3. 验证 urls 以检查服务器是否响应 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