-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (48 loc) · 1.6 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
var http = require('http');
var fs = require('fs');
var helloString = "{{ user_defined.welcome_msg }} <br />. My hostname at the time of the rendering was: {{ facts.hostname }}";
var dataFile = '{{ user_defined.data_file }}';
var connectionCount = 0;
function logVisitorData(ipAddress, date) {
var stringToWrite = ipAddress + '|' + date + '\n';
var stream = fs.createWriteStream(dataFile, {'flags': 'a'});
stream.write(stringToWrite, 'utf8');
}
function getVisitorsString(callback) {
var data = '';
var stream = fs.createReadStream(dataFile);
stream.on('data', function(chunk) {
data += chunk;
});
stream.on('error', function(err) {
callback('');
});
stream.on('end', function() {
var i, visitors, visitor, split;
var returnData = '';
var visitors = data.split('\n');
for (i = 0; i < visitors.length; i++) {
visitor = visitors[i];
split = visitor.split('|');
if (split.length != 2) {
continue;
}
returnData += split[1] + ': ' + split[0] + '<br />';
}
callback(returnData);
});
}
http.createServer(function (req, res) {
var ipAddress = req.socket.remoteAddress;
var date = new Date();
var msgString = helloString;
connectionCount++;
console.log('Client ' + connectionCount + ' (' + ipAddress + ') connected.');
getVisitorsString(function inVisitorData(visitors) {
res.writeHead(200, {'Content-Type': 'text/html'});
msgString += '<br /> Previous visitors: <br />';
msgString += visitors;
res.end(msgString);
});
logVisitorData(ipAddress, date);
}).listen({{ user_defined.server_port }}, "0.0.0.0");