-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (85 loc) · 3.47 KB
/
server.js
File metadata and controls
85 lines (85 loc) · 3.47 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var fs = require('fs');
var http = require('http');
var url = require('url');
var ROOT_DIR = "./";
var vimcount = 0;
var emacscount = 0;
var windows = 0;
var mac = 0;
var linux = 0;
var before = 0;
var after = 0;
var pageloads = 0;
http.createServer(function(req, res) {
var urlObj = url.parse(req.url, true, false);
if (urlObj.pathname == "/vim") {
vimcount++;
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/emacs") {
emacscount++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/windows") {
windows++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/mac") {
mac++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/linux") {
linux++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/before") {
before++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/after") {
after++;
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/query") {
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
var jsonresult = { vim: vimcount, emacs: emacscount, windows: windows, mac: mac, linux: linux, before: before, after: after, pageloads: pageloads};
res.end(JSON.stringify(jsonresult));
} else if (urlObj.pathname == "/") {
fs.readFile(ROOT_DIR + /*urlObj.pathname*/"/votingLocked.html", function(err, data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
pageloads++;
res.writeHead(200);
res.end(data);
});
} else {
fs.readFile(ROOT_DIR + urlObj.pathname, function(err, data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
if (urlObj.pathname == "/voting.html") pageloads++;
res.writeHead(200);
res.end(data);
});
}
}).listen(8080);
console.log("Listening on port 8080");