-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.ping.js
73 lines (55 loc) · 1.42 KB
/
node.ping.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var http = require('http');
var request = require('request');
var opts = {
host: "127.0.0.1",
ping_port: 9876,
couch_port: 5984,
db_name: "ping"
};
var spacer = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
var headers404 = {
"Content-Type": "text/html",
"Content-Length": "0"
};
var pingHeaders = {
"Cache-Control": "private, no-store, no-cache, proxy-revalidate",
"Content-Type": "image/gif",
"Content-Disposition": "inline",
"Content-Length": 43
};
function init() {
startPingHost();
}
function startPingHost() {
http.createServer(function (req, res) {
if (req.url !== '/spacer.gif') {
res.writeHead(404, headers404);
res.end();
return false;
}
var buf = new Buffer(43);
buf.write(spacer, "base64");
res.writeHead(200, pingHeaders);
res.write(buf);
res.end();
writeStats(req.headers);
}).listen(opts.ping_port, opts.host);
console.log('Ping server running at http://' + opts.host + ':' + opts.ping_port);
};
function writeStats(headers) {
var stats = {
date: JSON.stringify(new Date()),
headers: {
'user-agent' : headers['user-agent']
}
};
request({
method: 'POST',
headers: {'Content-Type': 'application/json'},
uri: "http://" + opts.host + ":" + opts.couch_port + "/" + opts.db_name,
body: JSON.stringify(stats)
}, function(err, resp, body) {
console.log(body);
});
}
init();