forked from iloire/watchmen-ping-http-head
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (42 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var request = require('request');
function PingService() {
}
exports = module.exports = PingService;
PingService.prototype.ping = function (service, callback) {
var startTime = +new Date();
var options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
},
uri: service.url,
port: service.port,
family: 4,
method: 'HEAD',
timeout: service.timeout
};
var expectedStatusCode = 200;
console.log(service.port);
var serviceOptions = (service.pingServiceOptions && service.pingServiceOptions['http-head']) || {};
if (serviceOptions.statusCode && serviceOptions.statusCode.value) {
expectedStatusCode = parseInt(serviceOptions.statusCode.value, 10);
}
request(options, function (error, response, body) {
if (error) {
return callback(error, body, response, +new Date() - startTime);
}
if (response && response.statusCode != expectedStatusCode) {
var errMsg = 'Invalid status code. Found: ' + response.statusCode +
'. Expected: ' + expectedStatusCode;
return callback(errMsg, body, response, +new Date() - startTime);
}
callback(null, body, response, +new Date() - startTime);
});
};
PingService.prototype.getDefaultOptions = function () {
return {
'statusCode': {
descr: 'Expected status code (defaults to 200)',
required: false
}
};
};