-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (40 loc) · 1.13 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
function getFileName(pathname) {
switch(pathname) {
case('/'):
return 'index.html'
case('/index'):
return 'index.html'
case('/about'):
return 'about.html'
case('/contact-me'):
return 'contact-me.html'
case('/myCode'):
return 'index.js'
default:
return '404.html'
}
}
const server = http.createServer((req, res) => {
const pathname = url.parse(req.url, true).pathname;
const fileName = getFileName(pathname);
let filePath = path.join(
__dirname,
fileName
);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text-html" });
return res.end(`Error loading: ${err}`);
} else if (fileName === "404.html"){
res.writeHead(404, { "Content-Type": "text-html" });
return res.end(data);``
}
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
});
server.listen(8080)