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
74
75
76
77
78
79
|
var os = require('os');
var http = require('http');
var ps = require('portscanner');
var dispatcher = require('httpdispatcher');
var PORT = 3369;
var serverstat={
"release":"undef",
"uptime":"",
"load":[0,0,0],
"mem":0,
"mc":false
}
serverstat.release = os.release();
function secondsToString(seconds)
{
function adds(x,a){
return x + " " + a + " ";
}
var numdays = Math.floor(seconds / 86400); seconds%=86400;
var numhours = Math.floor(seconds / 3600); seconds%=3600;
var numminutes = Math.floor(seconds / 60); seconds%=60;
return adds(numdays,"day") + adds(numhours,"hrs")+ adds(numminutes,"min") + adds(seconds,"sec");
}
function refreshStat(){
ps.checkPortStatus(25565, 'cnjoe.info', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
if (status=='open')
serverstat.mc=true;
else
serverstat.mc=false;
})
serverstat.uptime = os.uptime();
serverstat.mem = 1 - os.freemem()/os.totalmem();
serverstat.load = os.loadavg();
}
function handleRequest(request, response){
try {
//log the request on console
console.log(request.url);
//Disptach
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
//For all your static (js/css/images/etc.) set the directory name (relative path).
dispatcher.setStatic('resources');
//A sample GET request
dispatcher.onGet("/stat", function(req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(serverstat));
});
//A sample POST request
//dispatcher.onPost("/post1", function(req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Got Post Data');
//});
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
refreshStat();
setInterval(refreshStat,5000);
|