-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (80 loc) · 2.84 KB
/
Copy pathserver.js
File metadata and controls
96 lines (80 loc) · 2.84 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
const http = require("http");
const path = require("path");
const { URL } = require("url");
const { loadBoard, createColumn, deleteColumn, addCard, moveCard, updateCard, deleteCard } = require("./src/store");
const { serveStatic, sendJson, readJsonBody } = require("./src/http");
const publicDir = path.join(__dirname, "public");
function parseIdFromPath(pathname, prefix) {
if (!pathname.startsWith(prefix)) {
return null;
}
return pathname.slice(prefix.length);
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const { pathname } = url;
try {
if (req.method === "GET" && pathname === "/api/board") {
const board = await loadBoard();
return sendJson(res, 200, board);
}
if (req.method === "POST" && pathname === "/api/columns") {
const body = await readJsonBody(req);
const board = await createColumn(body.title);
return sendJson(res, 201, board);
}
if (req.method === "DELETE") {
const columnId = parseIdFromPath(pathname, "/api/columns/");
if (columnId) {
const board = await deleteColumn(columnId);
return sendJson(res, 200, board);
}
}
if (req.method === "POST" && pathname === "/api/cards") {
const body = await readJsonBody(req);
const board = await addCard(body.columnId, body.title, body.description || "");
return sendJson(res, 201, board);
}
if (req.method === "PATCH" && pathname === "/api/cards/move") {
const body = await readJsonBody(req);
const board = await moveCard(body.cardId, body.toColumnId, body.toIndex);
return sendJson(res, 200, board);
}
if (req.method === "PATCH") {
const cardId = parseIdFromPath(pathname, "/api/cards/");
if (cardId) {
const body = await readJsonBody(req);
const board = await updateCard(cardId, body);
return sendJson(res, 200, board);
}
}
if (req.method === "DELETE") {
const cardId = parseIdFromPath(pathname, "/api/cards/");
if (cardId) {
const board = await deleteCard(cardId);
return sendJson(res, 200, board);
}
}
if (req.method === "GET" && pathname === "/favicon.ico") {
res.writeHead(204);
res.end();
return;
}
if (req.method === "GET" && pathname === "/") {
await serveStatic(res, publicDir, "index.html");
return;
}
if (req.method === "GET") {
await serveStatic(res, publicDir, pathname);
return;
}
return sendJson(res, 404, { error: "Not found" });
} catch (error) {
const status = error.statusCode || 500;
return sendJson(res, status, { error: error.message || "Internal server error" });
}
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Kanban board is running on http://localhost:${port}`);
});