在node.js中只需要几行代码就可以发送http请求,通过检查statusCode可以判断该网址服务是否有效。
如下代码:
var http = require('http')
var checkExists = function(urlPath, cb) {
var options = { host: '192.168.2.101', port: 8064, path: encodeURI(urlPath)}
var req = http.request(options, function(r) {
cb && cb(r.statusCode == 200);
})
req.end()
}
以上代码检查相对网址是否可用,需手动修改服务器地址和端口,使用以下代码可以检查完整地址的可用性。
var http = require('http')
var url = require('url')
var checkUrlExists = function (Url, cb) {
var options = {
//method: 'HEAD',
host: url.parse(Url).host,
port: 80,
path: url.parse(Url).pathname
};
var req = http.request(options, function (r) {
cb && cb( r.statusCode == 200);
});
req.end();
}
搭配 fs 模块,可以指解析文件中的地址是否可用,完整代码如下:
var http = require('http')
var url = require('url')
var fs = require('fs')
/*
检查完整网址是否可用
*/
var checkUrlExists = function (Url, cb) {
var options = {
//method: 'HEAD',
host: url.parse(Url).host,
port: 80,
path: url.parse(Url).pathname
};
var req = http.request(options, function (r) {
cb && cb( r.statusCode == 200);
});
req.end();
}
/*
检查相对网址是否可用,需修改服务器地址和端口
*/
var checkExists = function(urlPath, cb) {
var options = { host: '192.168.2.101', port: 8064, path: encodeURI(urlPath)}
var req = http.request(options, function(r) {
cb && cb(r.statusCode == 200);
})
req.end()
}
var checkFile = function(filePath) {
fs.readFile(filePath, function(err, data) {
if (err) {
console.log(err)
return
}
var lines = data.toString().split(/[\r\n]+/)
var curr = 0
var urlPath
var checkNext = function(exist) {
if (curr > lines.length - 1) {
console.log('end')
return
}
//console.log(curr, 'exist', exist, urlPath)
if (!exist) {
console.log(curr, 'exist', exist, urlPath)
}
if (curr >= lines.lines) {
console.log('end')
return
}
urlPath = lines[curr++]
checkExists(urlPath, checkNext)
}
checkNext()
})
}
checkFile('./visit.csv')
回复 (0)
微信扫码 立即评论