-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (34 loc) · 1.09 KB
/
Copy pathserver.js
File metadata and controls
38 lines (34 loc) · 1.09 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
const http = require("http");
const {
getBooks,
getBook,
createBook,
updateBook,
deleteBook,
} = require("./controllers/bookController");
const server = http.createServer((req, res) => {
if (req.url === "/books" && req.method === "GET") {
getBooks(req, res);
} else if (req.url.match(/\/books\/\w+/) && req.method === "GET") {
const id = req.url.split("/")[2];
getBook(req, res, id);
} else if (req.url === "/books" && req.method === "POST") {
createBook(req, res);
} else if (req.url.match(/\/books\/\w+/) && req.method === "PUT") {
const id = req.url.split("/")[2];
updateBook(req, res, id);
} else if (req.url.match(/\/books\/\w+/) && req.method === "DELETE") {
const id = req.url.split("/")[2];
deleteBook(req, res, id);
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
message: "Route Not Found: Please use the books endpoint",
})
);
}
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
module.exports = server;