-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
43 lines (36 loc) · 1.21 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
var request = require('request');
function PingService() {
}
exports = module.exports = PingService;
PingService.prototype.ping = function (service, callback) {
var startTime = +new Date();
var options = {
method: 'HEAD',
uri: service.url,
timeout: service.timeout
};
var expectedStatusCode = 200;
var serviceOptions = (service.pingServiceOptions && service.pingServiceOptions['http-head']) || {};
if (serviceOptions.statusCode && serviceOptions.statusCode.value) {
expectedStatusCode = parseInt(serviceOptions.statusCode.value, 10);
}
request.get(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
}
};
};