-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservers.js
More file actions
58 lines (51 loc) · 1.28 KB
/
servers.js
File metadata and controls
58 lines (51 loc) · 1.28 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
const http= require('http');
const fs = require('fs');
const _ = require('lodash');
const server= http.createServer((req,res) => {
console.log(req.url, req.method);
//lodash
const num = _.random(12, 36);
console.log(num);
//runs only once
const greet = _.once(() => {
console.log("Greetings");
});
greet();
greet();
res.setHeader('Content-Type', 'text/html');
// res.write('<head>Creating a site</head>')
// res.write('<h1>Server is on</h1>');
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.setHeader('Location', '/about');
res.statusCode=301;
res.end();
break;
default:
path += '404.html';
res.statusCode=404;
break;
}
//sending html file
fs.readFile(path, (err, data) => {
if(err){
console.log(err);
}
else{
res.write(data);
res.end();
}
});
});
server.listen(3000, 'localhost', () => {
console.log('Listening');
})