Repository navigation
Expand file tree
/
Copy pathhttp-server.js
More file actions
183 lines (154 loc) · 6.23 KB
/
Copy pathhttp-server.js
File metadata and controls
183 lines (154 loc) · 6.23 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// first import the basic http module like last time
// also import 'fs' to use the file system (because this time
// we want to send our files to the user)
var http = require('http');
var fs = require('fs');
var DATA = [
{
name: 'Lukas',
hours: 150,
},
{
name: 'Lara',
hours: 120,
},
{
name: 'Tom',
hours: 115,
},
{
name: 'Marc',
hours: 7,
},
{
name: 'Anne',
hours: 22,
},
];
// still the same as last time, we make the server
var server = http.createServer(function (req, res) {
// first big difference: last time we sent back the same response
// no matter the request
// this time we have to check the path that the user requests
// like tech-academy.io/path/morepath/...
// "/" is the root path, it does not have to be entered by the user
// so we send back out ".html" file for the root request
if (req.url === "/") {
// this function can read files from the file system
fs.readFile("public/index.html", function (error, filecontent) {
// we need to check for errors, else the entire server would
// crash on every error that could occur
if (error) {
// this is where the "status codes" come from, like the
// famous 404
// specifying the codes is not required, but the browser
// can use them internally for optimizations
res.writeHead(404);
res.write('File not found');
} else {
// in addition to the status, we can/should send additional
// information about what we are sending back, otherwise the
// browser might have problems processing them
// a ".html" file has the type "text/html"
res.writeHead(200, { 'Content-Type': 'text/html' });
// the actual file content then has to be "written" which
// basically comes from the idea that the content is not sent
// as an entire file, but "written" to a stream of data
// sending one monolithic file of X MB would not be functional
// on the internet .. not totally important why at this point
res.write(filecontent);
}
// since we have created a stream of data, we need to specify when
// the stream is over. this is how
res.end();
});
// now it gets interesting: inside our ".html" file we have linked to our
// stylesheet and our javascript file. when the browser receives our html file
// it reads it top to bottom, line by line and tries to execute what it reads.
// when it gets to the link to our stylesheet, it tries to resolve the path
// that is specified and thus sends a new request to the server, asking
// for the file that is at "/styles.css". so it asks for the root path "/"
// plus the path to the file we want. therefore we have to handle this
// exact request. everything inside this case is the same as before
} else if (req.url === "/styles.css") {
fs.readFile("public/styles.css", function (error, filecontent) {
if (error) {
res.writeHead(404);
res.write('File not found');
} else {
// small difference here: this file is not html, it is css
res.writeHead(200, { 'Content-Type': 'text/css' });
res.write(filecontent);
}
res.end();
});
// same as with the css file, when the browser reaces the link to our
// script file, it will request this as well
} else if (req.url === "/script.js") {
fs.readFile("public/script.js", function (error, filecontent) {
if (error) {
res.writeHead(404);
res.write('File not found');
} else {
// obviously this has the type javascript
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.write(filecontent);
}
res.end();
});
// to show the possibilities, i have included a custom font. this font is not
// linked in the html file, it is linked from the css. the browser will request
// this file just the same. note that this is inside an asset-folder, therefore
// the requested path has to include this folder
} else if (req.url === "/assets/Montserrat-Bold.ttf") {
fs.readFile("public/assets/Montserrat-Bold.ttf", function (error, filecontent) {
if (error) {
res.writeHead(404);
res.write('File not found');
} else {
res.writeHead(200, { 'Content-Type': 'text/ttf' });
res.write(filecontent);
}
res.end();
});
} else if (req.url === "/assets/Montserrat-Regular.ttf") {
fs.readFile("public/assets/Montserrat-Regular.ttf", function (error, filecontent) {
if (error) {
res.writeHead(404);
res.write('File not found');
} else {
res.writeHead(200, { 'Content-Type': 'text/ttf' });
res.write(filecontent);
}
res.end();
});
// images work just the same
} else if (req.url === "/assets/logo.png") {
fs.readFile("public/assets/logo.png", function (error, filecontent) {
if (error) {
res.writeHead(404);
res.write('File not found');
} else {
// this content-type could be made into something dynamic
// the part "image/png" is also called a mime-type
res.writeHead(200, { 'Content-Type': 'image/png' });
res.write(filecontent);
}
res.end();
});
} else if (req.url === "/data") {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(DATA));
res.end();
} else {
// so if none of the paths can be matched, we need a defualt case
// so that our server doesn't break on error
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<h1>Path not found!</h1>' + req.url);
res.end();
}
});
// start up the server as usual
server.listen(1337);
// a small message to see the process is running
console.log('Server Started listening on port 1337');