-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (40 loc) · 1.03 KB
/
server.js
File metadata and controls
48 lines (40 loc) · 1.03 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
//This file is the pure node.js code to create a server and reply to response.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
// set header content type to define the type of data sent back to browswer
res.setHeader('Content-Type', 'text/html');
// routing
let path = './views/';
switch(req.url) {
case '/':
path += 'index.html';
res.statusCode = 200;
break;
case '/about':
path += 'about.html';
res.statusCode = 200;
break;
case '/about-us':
res.statusCode = 301;
res.setHeader('Location', '/about');
res.end();
break;
default:
path += '404.html';
res.statusCode = 404;
}
// send html
fs.readFile(path, (err, data) => {
if (err) {
console.log(err);
res.end();
}
//res.write(data);
res.end(data);
});
});
// localhost is the default value for 2nd argument
server.listen(3000, 'localhost', () => {
console.log('listening for requests on port 3000');
});