Repository navigation
Expand file tree
/
Copy pathexample4.js
More file actions
58 lines (46 loc) · 1.59 KB
/
Copy pathexample4.js
File metadata and controls
58 lines (46 loc) · 1.59 KB
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
// 192.168.16.13
var http = require("http"),
client = require("redis").createClient();
function copy_stats(reply) {
var ret = {};
Object.keys(reply).forEach(function (key) {
ret[key] = reply[key].toString();
});
return ret;
}
function handle_client(port, request, response) {
var footer, slide_list, request_count, start = Date.now(), stats = {};
response.writeHead(200, {
"Content-Type": "text/html"
});
client.mget("page_header.html", "page_footer.html", function (err, reply) {
response.write(reply[0]); // header
footer = reply[1].toString();
});
client.hincrby("port", port, 1);
client.hincrby("url", request.url, 1);
client.hincrby("ip", request.connection.remoteAddress, 1);
client.hgetall("port", function (err, reply) {
stats.port = copy_stats(reply);
});
client.hgetall("url", function (err, reply) {
stats.url = copy_stats(reply);
});
client.hgetall("ip", function (err, reply) {
stats.ip = copy_stats(reply);
response.write(JSON.stringify(stats));
response.write(footer);
response.end();
client.publish("access log:" + port, request.connection.remoteAddress + " " +
request.url + " " + (request.headers["user-agent"] || ""));
});
}
function get_handler(port) {
return function (request, response) {
handle_client(port, request, response);
}
}
for (var port = 9000; port <= 9999; port += 1) {
console.log("Listening on " + port);
http.createServer(get_handler(port)).listen(port);
}