-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (50 loc) · 1.09 KB
/
server.js
File metadata and controls
68 lines (50 loc) · 1.09 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
58
59
60
61
62
63
64
65
66
67
68
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(()=>{
console.log('hey')
})
greet();
greet();
//set header content type
res.setHeader('content-Type', 'text/html');
//path
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-ms': //redirect
res.statusCode = 301;
res.setHeader('Location', '/about');
res.end();
break;
default:
path += '404.html'
res.statusCode = 404;
break;
}
//sender html file
fs.readFile(path, (err,data)=>{
if(err){
console.log(err)
res.end();
}else{
//res.write(data);
res.end( data);
}
})
});
server.listen(3000, 'localhost', ()=>{
console.log('listening for request on port 3000');
})