-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (123 loc) · 4.91 KB
/
app.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
//app.js
const http = require("http");
const Todo = require("./controller");
const { getReqData } = require("./utils");
const PORT = process.env.PORT || 8080;
const HOST = "localhost";
const server = http.createServer(async (req, res) => {
console.log("Called" + req.method + " : " + req.url);
if (req.url === "/" && req.method === "GET") {
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the data
res.end("Welcome, this is your Home page\n");
}
//Health check and uptime endpoint
else if (req.url === "/health" && req.method === "GET") {
// console.log("Called GET : 0.0.0.0:8080/health");
const healthcheck = {
uptime: process.uptime(),
message: "OK",
timestamp: Date.now(),
};
res.end(JSON.stringify(healthcheck));
}
// /api/todos : GET
else if (req.url === "/api/todos" && req.method === "GET") {
// console.log("Called GET : 0.0.0.0:8080/api/todos");
// get the todos.
const todos = await new Todo().getTodos();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the data
res.end(JSON.stringify(todos));
}
// /api/todos/:id : GET
else if (req.url.match(/\/api\/todos\/([0-9]+)/) && req.method === "GET") {
// console.log("Called GET : 0.0.0.0:8080/api/todos/{id}");
try {
// get id from url
const id = req.url.split("/")[3];
// get todo
const todo = await new Todo().getTodo(id);
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the data
res.end(JSON.stringify(todo));
} catch (error) {
// set the status code and content-type
res.writeHead(404, { "Content-Type": "application/json" });
// send the error
res.end(JSON.stringify({ message: error }));
}
}
// /api/todos/:id : DELETE
else if (
req.url.match(/\/api\/todos\/([0-9]+)/) &&
req.method === "DELETE"
) {
// console.log("Called DELETE : 0.0.0.0:8080/api/todos/{id}");
try {
// get the id from url
const id = req.url.split("/")[3];
// delete todo
let message = await new Todo().deleteTodo(id);
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the message
res.end(JSON.stringify({ message }));
//or response status = 204 if no response body is sent
// res.writeHead(204, { "Content-Type": "application/json" });
} catch (error) {
// set the status code and content-type
res.writeHead(404, { "Content-Type": "application/json" });
// send the error
res.end(JSON.stringify({ message: error }));
}
}
// /api/todos/:id : UPDATE
else if (
req.url.match(/\/api\/todos\/([0-9]+)/) &&
req.method === "PATCH"
) {
// console.log("Called PATCH : 0.0.0.0:8080/api/todos/{id}");
try {
// get the id from the url
const id = req.url.split("/")[3];
// update todo
let updated_todo = await new Todo().updateTodo(id);
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the message
res.end(JSON.stringify(updated_todo));
} catch (error) {
// set the status code and content type
res.writeHead(404, { "Content-Type": "application/json" });
// send the error
res.end(JSON.stringify({ message: error }));
}
}
// /api/todos/ : POST
else if (req.url === "/api/todos" && req.method === "POST") {
// console.log("Called POST : 0.0.0.0:8080/api/todos");
// get the data sent along
let todo_data = await getReqData(req);
// create the todo
let todo = await new Todo().createTodo(JSON.parse(todo_data));
// set the status code and content-type
res.writeHead(201, { "Content-Type": "application/json" }); //was 200
//send the todo
res.end(JSON.stringify(todo));
}
// No route present
else {
console.warn(
"This endpoint is not implemented / unavailable at the moment !!"
);
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found" }));
}
});
server.listen(PORT, () => {
console.log(`server started on ${HOST} port: ${PORT}`);
});