forked from RVCC-IDMX/node-server-tarunbalaji3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (31 loc) · 743 Bytes
/
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
35
36
37
38
const http = require("http");
const fs = require("fs");
const PORT = process.env.PORT || 3000
const server = http.createServer((req, res) => {
console.log(req.url, req.method);
res.setHeader("Content-Type", "text/html");
let path = './';
switch (req.url) {
case '/':
path += 'index.html';
res.statusCode = 200;
break;
case '/about':
path += 'about.html';
res.statusCode = 200;
break;
default:
path += '404.html';
res.statusCode = 404;
break;
}
fs.readFile(path, (err, data) => {
if (err) {
console.log(err);
res.end();
} else {
res.end(data);
}
})
});
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`))