-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
109 lines (102 loc) · 3.99 KB
/
app.js
File metadata and controls
109 lines (102 loc) · 3.99 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
97
98
99
100
101
102
103
104
105
106
107
108
109
// app.js
const http = require('http');
// import functions made in other files
const Controller = require('./controllers');
const { getReqData } = require('./utils')
// if have enviornment set up then itll grap the port number
// from enviornment, else default is 8999
const PORT = process.env.PORT || 8999;
const server = http.createServer(async (req, res) => {
// set the request route
if (req.url === '/api' && req.method === 'GET') {
// response headers
// application/json is one content type
res.writeHead(200, {'Content-Type': 'application/json'});
// set the response
res.write('howdy, this is a vanilla Node.js API');
// end the response
res.end()
}
// /api/todos : GET
else if (req.url === "/api/todos" && req.method === "GET") {
// get the todos.
const todos = await new Controller().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") {
try {
// get id from url
const id = req.url.split("/")[3];
// get todo
const todo = await new Controller().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") {
try {
// get the id from url
const id = req.url.split("/")[3];
// delete todo
let message = await new Controller().deleteTodo(id);
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json" });
// send the message
res.end(JSON.stringify({ message }));
} 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") {
try {
// get the id from the url
const id = req.url.split("/")[3];
// update todo
let updated_todo = await new Controller().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") {
// get the data sent along
let todo_data = await getReqData(req);
// create the todo
let todo = await new Controller().createTodo(JSON.parse(todo_data));
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json" });
//send the todo
res.end(JSON.stringify(todo));
}
// No route present
else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Route not found" }));
}
})
server.listen(PORT, () => {
console.log(`server started on port: ${PORT}`);
})