-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
175 lines (169 loc) · 4.84 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
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
var http = require("http")
var url = require("url");
var path = require("path");
var fs = require("fs");
var qs = require('querystring');
var config = require('./config');
function e500(response) {
fs.readFile(process.cwd() + '/html/500.html', "binary", function(err, file) {
if (err) {
response.writeHead(500, {
"Content-Type": "text/plain"
});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
}
function e404(response) {
fs.readFile(process.cwd() + '/html/404.html', "binary", function(err, file) {
if (err) {
e500(response);
return;
}
//out file
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
}
function e403(response) {
fs.readFile(process.cwd() + '/html/403.html', "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
}
function e200(response) {
fs.readFile(process.cwd() + '/html/200.html', "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
}
function dbdl(response) {
fs.readFile(process.cwd() + '/database.csv', "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200, {
"Content-Type": "application/octet-stream"
});
response.write(file, "binary");
response.end();
});
}
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
if ((uri.startsWith("/js") || uri.startsWith("/css")) && (uri.endsWith(".js") || uri.endsWith(".css"))) {
var filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
if (!exists) {
e404(response);
return;
}
fs.readFile(filename, "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
} else if (uri == "/") {
if (request.method == "GET") {
var filename = process.cwd() + '/html/quest.html';
fs.readFile(filename, "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
} else if (request.method == "POST") {
var body = '';
request.on('data', function(data) {
body += data;
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function() {
var post = qs.parse(body);
var csvLine = "";
//fs.appendFile('database.json', JSON.stringify(post, null, "\t"),function(){});
for (var key in post) {
csvLine += post[key] + ",";
}
csvLine = csvLine.substring(0, csvLine.length - 1);
fs.appendFile('database.csv', csvLine + '\n', function(err) {
console.log(err);
});
e200(response);
});
} else {
e404(response);
return;
}
} else if (uri == "/dwn" && request.method == "GET") {
var filename = process.cwd() + '/html/dwn.html';
fs.readFile(filename, "binary", function(err, file) {
if (err) {
e500(response);
return;
}
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write(file, "binary");
response.end();
});
} else if (uri == "/dwn.csv" && request.method == "POST") {
var body = '';
request.on('data', function(data) {
body += data;
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function() {
var post = qs.parse(body);
if (post["code"] == config.password) {
dbdl(response);
return;
} else {
e403(response);
return;
}
});
} else {
e404(response);
return;
}
}).listen(parseInt(config.port, 10));
console.log("Server running at http://localhost:" + config.port + "/");