-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_server.js
117 lines (99 loc) · 3.21 KB
/
05_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
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
// const http = require("http");
// const port = process.env.PORT || 1337;
// const querystring = require("querystring");
// const fs = require("fs");
// function respondText(req, res) {
// res.setHeader("Content-Type", "text/plain");
// res.end("Hello");
// }
// function respondJson(req, res) {
// res.setHeader("Content-Type", "application/json");
// res.end(JSON.stringify({ text: "Hi", number: [1, 2, 3] }));
// }
// function respondNotFound(req, res) {
// res.writeHead(404, { "Content-Type": "text/plain" });
// res.end("Not Found");
// }
// // Dynamic Responses
// function respondEcho(req, res) {
// const { input = "" } = querystring.parse(
// req.url.split("?").splice(1).join("")
// );
// res.setHeader("Content-Type", "application/json");
// res.end(
// JSON.stringify({
// // normal, shouty, characterCount, backwards
// normal: input,
// shouty: input.toUpperCase(),
// characterCount: input.length,
// backwards: input.split("").reverse().join(""),
// })
// );
// }
// // File Serving
// function respondStatic(req, res) {
// const filename = `${__dirname}/public${req.url.split("/static")[1]}`;
// fs.createReadStream(filename)
// .on("error", () => respondNotFound(req, res))
// .pipe(res);
// }
// // Server
// const server = http.createServer((req, res) => {
// if (req.url === "/") return respondText(req, res);
// if (req.url === "/json") return respondJson(req, res);
// if (req.url.match(/^\/echo/)) return respondEcho(req, res);
// if (req.url.match(/^\/static/)) return respondStatic(req, res);
// respondNotFound(req, res);
// });
// server.listen(port);
// console.log(`Listening to port ${port}`);
const http = require("http");
const port = process.env.PORT || 1337;
const querystring = require("querystring");
const fs = require("fs");
// PLAIN-TEXT RESPONSE
function respondText(req, res) {
res.setHeader("Content-Type", "text/plain");
res.end("Hello User");
}
// JSON RESPONSE
function respondJson(req, res) {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ name: "John Doe" }));
}
// 404 RESPONSE
function respondNotFound(req, res) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
}
// DYNAMIC RESPONSE
function respondEcho(req, res) {
const { input = "" } = querystring.parse(
req.url.split("?").splice(1).join("")
);
res.setHeader("Content-Type", "application.json");
res.end(
JSON.stringify({
normal: input,
shouty: input.toUpperCase(),
characterCount: input.length,
backwards: input.split("").reverse().join(""),
})
);
}
// FILE SERVING
function respondStatic(req, res) {
const filename = `${__dirname}/public${req.url.split("/static")[1]}`;
fs.createReadStream(filename)
.on("error", () => respondNotFound(req, res))
.pipe(res);
}
const server = http.createServer((req, res) => {
if (req.url === "/") return respondText(req, res);
if (req.url === "/json") return respondJson(req, res);
if (req.url.match(/^\/echo/)) return respondEcho(req, res);
if (req.url.match(/^\/static/)) return respondStatic(req, res);
respondNotFound(req, res);
});
server.listen(port);
console.log(`Server listening on port ${port}`);