简体   繁体   English

使用Node.js测试URL

[英]Testing URLs with Node.js

Assuming I have an array of URLs and I want to ensure each URL is working I have created the following code. 假设我有一个URL数组,并且想确保每个URL都能正常工作,我创建了以下代码。 However only the last URL in the array is getting tested. 但是,仅对数组中的最后一个URL进行了测试。 How can I ensure each url returns a 200 response code? 如何确保每个网址都返回200响应代码? To be clear these are all remote addresses I am testing and they point to decently sized PDFs. 为了清楚起见,这些都是我正在测试的远程地址,它们指向尺寸合适的PDF。

Updated based on @lukas.pukenis's response. 根据@ lukas.pukenis的回复进行了更新。 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

This is because your loop rewrites l variable each time with var l = fileLinks[link]; 这是因为您的循环每次使用var l = fileLinks[link];重写l变量var l = fileLinks[link];

so l has a value of the last value of array. 所以l的值是数组的最后一个值。 In order to preserve unique l value you need to store it somewhere. 为了保留唯一的l值,您需要将其存储在某个地方。 Better - function. 更好-功能。 Like this: 像这样:

function check(l) {
  var req = http.get(l, function(res) {
    if (res.statusCode != 200) {
      console.log(res.statusCode + ' on '+l);
    } else {
      console.log('success on ' + l);
    }
  }

  req.on('close', function() {
    console.log('Request done');
  });

for (link in fileLinks) {
  var l = fileLinks[link];
  check(l);
}

Having a function is no magic here. 在这里拥有功能并不是魔术。 It just preserves your local values in memory for each function call, so l is unique each time it needs to be. 它只是将本地值保存在每个函数调用的内存中,因此l每次需要时都是唯一的。

The for expression shouldn't be used with arrays. for表达式不应与数组一起使用。 Replace the for loop for something like this: for循环替换for以下内容:

fileLinks.forEach(function(item){
  check(item);
});

When doing this many outgoing requests, you may want to increase the maxSockets to something greater than 5, the default , otherwise you might get unexpected behavior. 在执行许多此外发请求时,您可能希望将maxSockets增加到大于5 的值即default ,否则可能会出现意外的行为。 Do this after you require('http') : 在您require('http')之后执行此操作:

http.globalAgent.maxSockets = 150;

Also, when you stick your console.log outside the callback function, it's not going to be displayed at the same time the response comes back from the server. 同样,当您将console.log在回调函数之外时,将不会在服务器返回响应的同时显示它。 It's redundant anyway. 无论如何都是多余的。 Here is a complete working example: 这是一个完整的工作示例:

var http = require('http');
var url = require('url');

function check(l) {
  var u = url.parse(l);

  var opts = {
    host: u.host,
    path: u.path,
    agent: false // prevents pooling behavior
  };

    http.get(opts, function(res) {
        if (res.statusCode != 200) {
            console.log(res.statusCode + ' on '+l);
        } else {
            console.log('success on ' + l);
        }
    });
}

fileLinks = ['http://www.google.com','http://www.google.com'];

fileLinks.forEach(function(item){
  check(item);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM