This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (191 loc) · 4.83 KB
/
server.js
File metadata and controls
212 lines (191 loc) · 4.83 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var http = require("http");
var https = require("https");
var fs = require("fs");
var ws = require("ws");
var path = require("path");
var URL = require("url");
var fakeIO = require("./fakeio-server.js");
var hosts = {};
var tempNumberIdThing = 0;
function setNoCorsHeaders(res) {
res.setHeader("Access-Control-Allow-Origin", "*");
/*res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);*/
}
function runStaticStuff(req, res, forceStatus) {
var url = URL.parse(req.url);
var pathname = url.pathname;
setNoCorsHeaders(res);
var file = path.join("./static/", pathname);
if (pathname == "/") {
file = "static/index.html";
}
if (file.split(".").length < 2) {
file += ".html";
}
if (!fs.existsSync(file)) {
file = "errors/404.html";
res.statusCode = 404;
}
if (typeof forceStatus !== "undefined") {
file = "errors/" + forceStatus + ".html";
res.statusCode = forceStatus;
}
fs.createReadStream(file).pipe(res);
}
function waitForBody(req) {
return new Promise((accept, reject) => {
var data = [];
req.on("data", (chunk) => {
data.push(chunk);
});
req.on("end", () => {
accept(Buffer.concat(data));
});
req.on("error", () => {
reject();
});
});
}
function createRandomCharsString(length) {
var keys = "ABCDEFGHIJKLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
var key = "";
var i = 0;
while (i < length) {
key += keys[Math.round(Math.random() * (keys.length - 1))];
i += 1;
}
return key;
}
var savedPolls = {};
const server = http.createServer(async function (req, res) {
var url = decodeURIComponent(req.url);
var urlsplit = url.split("/");
setNoCorsHeaders(res);
if (urlsplit[1] == "wake") {
res.end("");
return;
}
console.log(urlsplit,req.method);
if (urlsplit[1] == "newpoll" && req.method == "POST") {
(async function () {
try{
var body = await waitForBody(req);
var json = JSON.parse(body.toString());
var id = createRandomCharsString(10);
if (typeof json.question !== "string") {
res.end("");
return;
}
if (!Array.isArray(json.choices)) {
res.end("");
return;
}
for (var choice of json.choices) {
if (typeof choice !== "string") {
res.end("");
return;
}
}
var safePoll = {
question:json.question,
choices:json.choices,
votes: {}
};
savedPolls[id] = safePoll;
res.end(id);
}catch(e){
console.log("New poll error:",e);
res.end("");
}
})();
return;
}
if (urlsplit[1] == "getpoll" && req.method == "GET") {
if (!urlsplit[2]) {
res.statusCode = 404;
res.end("");
return;
}
var id = urlsplit[2];
if (savedPolls[id]) {
var poll = savedPolls[id];
var safePoll = {
question: poll.question,
choices: poll.choices,
};
res.end(JSON.stringify(safePoll));
} else {
res.statusCode = 404;
res.end("");
}
return;
}
if (urlsplit[1] == "getresults" && req.method == "GET") {
if (!urlsplit[2]) {
res.statusCode = 404;
res.end("");
return;
}
var id = urlsplit[2];
if (savedPolls[id]) {
var poll = savedPolls[id];
var safePoll = {
question: poll.question,
choices: poll.choices,
votes: poll.votes,
};
res.end(JSON.stringify(safePoll));
setTimeout(() => {
savedPolls[id] = undefined;
},1000*60);
} else {
res.statusCode = 404;
res.end("");
}
return;
}
if (urlsplit[1] == "pollvote" && req.method == "POST") {
(async function () {
try{
var body = await waitForBody(req);
var json = JSON.parse(body.toString());
console.log(json,urlsplit);
if (typeof json.index !== "number") {
res.end("");
return;
}
if (typeof json.username !== "string") {
res.end("");
return;
}
if (!urlsplit[2]) {
res.statusCode = 404;
res.end("");
return;
}
var id = urlsplit[2];
if (savedPolls[id]) {
var poll = savedPolls[id];
if (typeof poll.votes[json.index] == "undefined") {
poll.votes[json.index] = [];
}
poll.votes[json.index].push(json.username);
res.end("");
} else {
res.statusCode = 404;
res.end("");
}
}catch(e){
console.log("Vote poll error:",e);
res.end("");
}
})();
return;
}
runStaticStuff(req, res);
});
server.listen(8080);
console.log("Server started!");