-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (44 loc) · 1.32 KB
/
server.js
File metadata and controls
52 lines (44 loc) · 1.32 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
//I won't use this file but leave it like example
// where is everything done with NODE.js, without Express
const http = require('http');
const fs = require('fs');
const _ = require('lodash');
const server = http.createServer((req,res) => {
console.log(req.url, req.method);
// set header content type
res.setHeader('Content-type', 'text/html');
let path = './views/';
//we can use a switch statement to cycle through the different possible cases
switch(req.url) {
case '/':
path += 'index.html';
res.statusCode = 200;
break;
case '/about':
path += 'about.html';
res.statusCode = 200;
break;
case '/about-me':
res.statusCode = 301;
res.setHeader('Location', './about');
res.end();
break;
default:
path += '404.html';
res.statusCode = 404;
break;
}
// send an html file
fs.readFile(path, (err, data) => {
if (err) {
console.log(err);
res.end();
} else {
//res.write(data); - below is the shotest way of how to do that
res.end(data);
}
})
});
server.listen(3000, 'localhost', () => {
console.log('listening for requests on port 3000');
})