-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
72 lines (68 loc) · 1.81 KB
/
server.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
var async = require('async');
var http = require('http');
var port = process.env.PORT || 8080;
var user = process.env.FON_USER || 'RunnableBot';
var hostname = process.env.HOSTNAME || 'Unknown';
var Redis = require('redis');
var redis;
if (process.env.REDIS_HOSTNAME) {
redis = Redis.createClient(6379, process.env.REDIS_HOSTNAME);
redis.on('error', function (err) {
console.error('redis error:', err);
});
}
http.createServer(
function (req, res) {
req._count = -1;
req._path_count = -1;
async.series([
function countThings (cb) {
if (process.env.REDIS_HOSTNAME) {
incrPath(req.url);
redis.incr('thiskey', function (err, count) {
req._count = count || req._count;
cb(err);
});
} else {
cb();
}
},
function getPathCount (cb) {
if (process.env.REDIS_HOSTNAME) {
redis.hget('reqPaths', req.url, function (err, count) {
req._path_count = count || req._path_count;
cb(err);
});
} else {
cb();
}
},
function respond (cb) {
writeResponse(req, res);
cb();
}
], function (err) { });
}
).listen(port);
console.log('Server running at: http://127.0.0.1:' + port + '/');
function incrPath (path) {
redis.hincrby('reqPaths', path, 1, function (err) {
if (err) { console.error('redis err hincrby:', err); }
redis.hgetall('reqPaths', function (err, data) {
console.log(err, data);
});
});
}
function writeResponse (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end([
'Hello, ' + user + '!',
'',
'Served on port ' + port,
'By host ' + hostname,
'',
'All count: ' + req._count,
'Path count: ' + req._path_count,
''
].join('\n'));
}