-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
52 lines (45 loc) · 1.13 KB
/
index.mjs
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
import http from "http";
import App from "./service/app.mjs";
const app = new App();
const server = http.createServer(async function (req, res) {
res.setHeader("Content-Type", "application/json");
if (["POST", "PUT", "DELETE"].includes(req.method)) {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', async () => {
req.body = body ? JSON.parse(body) : {};
switch (req.method) {
case "POST":
app.create(req, res);
return;
//return app.create(req, res);
case "PUT":
return app.update(req, res);
case "DELETE":
return app.delete(req, res);
default:
break;
}
});
} else if (req.method === "GET") {
app.get(res);
} else {
res.statusCode = 405; // Method Not Allowed
res.write(JSON.stringify({
code: 405,
status: "error",
data: {
message: "Unknown request method."
}
}));
res.end();
}
});
try {
server.listen(8000);
console.log("App running on port 8000");
} catch (err) {
console.error("Error starting server:", err);
}