-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (54 loc) · 1.33 KB
/
index.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
// El modulo importado en la suguiente línea es el que debes crear.
const createServer = require("./create_server.js");
const get = (request, response) => {
const authorization = request.getHeader("Authorization");
if (authorization == null) {
response.send(
"200",
{ "content-Type": "application/json" },
JSON.stringify({
authenticated: true,
message: "Hello Authenticated User!",
})
);
} else {
response.send(
"200",
{ "Content-Type": "application/json" },
JSON.stringify({
authenticated: false,
message: "Hello Annonymous User!",
})
);
}
};
const requestListener = (request, response) => {
switch (request.method) {
case "GET": {
return get(request, response);
}
case "POST": {
return response.send(
202,
{ "Content-Type": "application/json" },
request.body
);
}
default: {
return response.send(
404,
{ "Content-Type": "text/plain" },
"The server only supports HTTP method GET"
);
}
}
};
const server = createServer((request, response) => {
try {
return requestListener(request, response);
} catch (error) {
console.error(error);
response.send(500, { "Content-Type": "text/plain" }, "Uncaught error");
}
});
server.listen(8080);