Javascript 使用Node.js测试URL
假设我有一个URL数组,并且我希望确保每个URL都正常工作,我已经创建了以下代码。但是,只有数组中的最后一个URL正在接受测试。如何确保每个url返回200响应代码?要明确的是,这些都是我正在测试的远程地址,它们指向大小适中的PDF 根据@lukas.pukenis的回复进行更新。结果相似,实际上只检查了少数文件Javascript 使用Node.js测试URL,javascript,node.js,Javascript,Node.js,假设我有一个URL数组,并且我希望确保每个URL都正常工作,我已经创建了以下代码。但是,只有数组中的最后一个URL正在接受测试。如何确保每个url返回200响应代码?要明确的是,这些都是我正在测试的远程地址,它们指向大小适中的PDF 根据@lukas.pukenis的回复进行更新。结果相似,实际上只检查了少数文件 function check(l) { console.log(l); http.get(l, function(res) { if (res.sta
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]);
}
此代码输出:
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
这是因为循环每次都用
var l=fileLinks[link]重写l变量代码>
所以l的值是数组的最后一个值。
为了保存唯一的l值,您需要将其存储在某个位置。更好的功能。像这样:
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);
}
拥有一个函数在这里不是什么魔法。它只是为每个函数调用保留内存中的本地值,因此l在每次需要时都是唯一的。for
表达式不应与数组一起使用。将for
循环替换为类似以下内容:
fileLinks.forEach(function(item){
check(item);
});
在处理这么多传出请求时,您可能希望将maxSockets
增加到大于5的值,否则可能会出现意外行为。请在要求('http')
后执行此操作:
此外,当您将console.log
粘贴到回调函数之外时,它不会在服务器返回响应的同时显示。反正是多余的。下面是一个完整的工作示例:
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);
});
谢谢你,卢卡斯。这有所帮助,但仍然无法解决问题。我更新了问题以反映您建议的编辑。您确定数组得到了正确的迭代吗?对于在里面循环用于对象i操作。请显示数据集或其结构:)它只是一个字符串数组。我认为它是循环的,因为.get调用之前的日志条目仍在为每条记录编写。我猜.get调用是在后台执行的,nodemon在关闭应用程序之前没有等待执行完成。如果根据我更新的代码修改代码,你能告诉我会发生什么吗?我已经读到,节点等待所有回调池都为空,您可以使用.on()添加事件,所以请尝试:)如果
fileLinks
是一个数组,请尝试使用.forEach(function(item){})
函数,而不是for
,后者用于循环遍历javascript对象中的各个键。它不适用于数组。你的例子和Lukas的一样有效。maxSockets设置会有所帮助,但在所有请求完成之前,应用程序仍会关闭。在您的示例中,处理了150个,然后又处理了12个左右,然后应用程序就关闭了。请尝试将agent
设置为false
以禁用池行为。呃,我错误地选择了false。我刚刚编辑了我的答案。试试看。
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);
});