-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
34 lines (30 loc) · 1.22 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
const http = require('http');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = process.env.PORT || 8080;
const server = http.createServer((request, response) => {
console.log('req.url = ', request.url);
if (request.url === '/') {
sendFileContent(response, "index.html", "text/html;charset=UTF-8");
} else if(/^\/[a-zA-Z0-9\/]*.js$/.test(request.url.toString())){
sendFileContent(response, request.url.toString().substring(1), "text/javascript");
} else if(/^\/[a-zA-Z0-9\/\.]*.css$/.test(request.url.toString())){
sendFileContent(response, request.url.toString().substring(1), "text/css");
} else if (request.url === '/favicon.ico') {
sendFileContent(response, "src/assets/img/favicon.ico", "image/png")
} else {
console.log("Requested URL is: " + request.url); // for debug purposes
sendFileContent(response, "404.html", "text/html;charset=UTF-8");
}
});
const sendFileContent = (response, fileName, contentType) => {
fs.readFile(fileName, (err, data) => {
response.statusCode = 200;
response.setHeader('Content-Type', contentType);
response.write(data);
response.end();
});
}
server.listen(port, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});