-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
53 lines (44 loc) · 1.12 KB
/
main.ts
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
// 3 things
// Persistent cookie for any user that accesess the website
//
// Database to store the tasks
//
// UI to create, change and delete tasks
import { createServer } from "node:http";
import { serverUrls } from "./controller";
import { createTable, createTask } from "./db_implementations";
const server = createServer((req, res) => {
if (req.method?.toLowerCase() === "post") {
if (!req.headers.cookie) {
const unauthorized = {
codeStatus: 401,
header: {
"content-type": "text/html",
}
}
res
.writeHead(unauthorized.codeStatus, unauthorized.header)
.end();
return;
}
let reqBody = '';
req.on("data", (buffer) => {
reqBody = buffer;
});
req.on("end", () => {
createTask(req.headers.cookie as string, JSON.parse(reqBody.toString()));
});
} else if (req.method?.toLowerCase() === "get") {
serverUrls(req).then((obtainedRes) => {
res
.writeHead(obtainedRes.codeStatus, obtainedRes.header)
.end(obtainedRes.body);
});
}
});
(async function initServer() {
await createTable();
server.listen(8080, () => {
console.log("HTTP server init successfully");
});
})();