-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (36 loc) · 1.09 KB
/
server.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
const http = require('http');
const getReq = require("./methods/get-request");
const postReq = require("./methods/post-request");
const putReq = require("./methods/put-request");
const deleteReq = require("./methods/delete-request");
let movies = require("./data/movies.json");
// require("dotenv").config();
const PORT = process.env.PORT || 5001;
const server = http.createServer((req, res) => {
req.movies = movies;
switch (req.method) {
case "GET":
getReq(req, res);
break;
case "POST":
postReq(req, res);
break;
case "PUT":
putReq(req, res);
break;
case "DELETE":
deleteReq(req, res);
break;
//caso padrao de default
default:
res.statusCode = 404;
res.setHeader("Content-Type", "application/json");
res.write(
JSON.stringify({ title:"Not Found", message: "Rout not found" })
);
res.end();
}
});
server.listen(PORT, () => {
console.log(`Server started on port: ${PORT}`);
});