-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsite.js
More file actions
57 lines (49 loc) · 1.89 KB
/
website.js
File metadata and controls
57 lines (49 loc) · 1.89 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
const http = require('http');
const fs = require('fs');
const arg = process.argv;
http.createServer((req, res) => {
// Best is sync approach
let collectHeaderData = fs.readFileSync('html/header.html', 'utf-8');
let collectedFooterData = fs.readFileSync('html/footer.html', 'utf-8');
// fs.readFile('html/header.html', 'utf-8', (err, headerData) => {
// if (err) {
// res.writeHead(500, { "content-type": 'text/plain' });
// res.end('Header not found');
// return;
// }
// collectHeaderData = headerData;
// // res.writeHead(200, { "content-type": 'text/html' });
// // res.end(headerData);
// });
// Node routing
let file = '/home';
if (req.url !== '/' && req.url !== '/style/style.css') {
file = req.url;
}
if (req.url === '/' || (!req.url.endsWith('.css') && !req.url.includes('.'))) {
let file = req.url === '/' ? '/home' : req.url;
fs.readFile("html" + file + ".html", 'utf-8', (err, data) => {
if (err) {
res.writeHead(500, { "content-type": 'text/plain' });
res.end('Internal server error');
return;
}
res.writeHead(200, { "content-type": 'text/html' });
res.write(collectHeaderData + "" + data + "" + collectedFooterData)
res.end();
});
} else if (req.url === '/style/style.css') {
fs.readFile('style/style.css', 'utf-8', (err, data) => {
if (err) {
res.writeHead(500, { "content-type": 'text/plain' });
res.end('Style not found');
return;
}
res.writeHead(200, { "content-type": 'text/css' });
res.end(data);
});
} else {
res.writeHead(404, { "content-type": 'text/plain' });
res.end('404 Not Found');
}
}).listen(arg[2]);