-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (49 loc) · 1.21 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const http = require('http');
const fs = require('fs');
const _ = require('lodash');
const server = http.createServer((req, res) => {
//lodash
const num = _.random(0,20);
console.log(num);
const greet = _.once(() => { // for executing only one time
console.log('hello');
})
greet();
greet(); //this second call will not be executed
console.log(req.url, req.method);
//set header content type
//res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Type', 'text/html');
// res.write('<p>hello,bro</p>');
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-me':
res.statusCode=301;
res.setHeader('Location', '/about');
break;
default:
path += '/404.html';
res.statusCode=404;
break;
}
//send HTML FILE
fs.readFile(path, (err,data) => {
if(err){
console.log(err);
res.end();
}else{
res.end(data);
}
})
});
server.listen(3000, 'localhost', () => {
console.log('listening for request in 3000')
})