-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticRoutes.js
82 lines (73 loc) · 2.05 KB
/
staticRoutes.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const http = require('http');
const fs = require('fs');
var server = http.createServer(incomeData);
server.on('listening', function(){
console.log(`Server listening on http://localhost:${server.address().port} - with PID: ${process.pid}`);
});
async function logRequest(message){
if(fs.existsSync('./messages.log'))
fs.appendFile('./messages.log', message, 'utf-8', function (error){
if(error)
console.error(error);
return;
});
return;
}
async function incomeData(req,res){
res.writeHeader(200,
{'Content-Type': 'text/html'}
);
if(req.method == 'GET'){
if(req.url == '/'){
var index = fs.createReadStream('./index.html',{encoding: 'utf-8'});
index.on('data', function(chunk){
if(chunk)
res.write(chunk);
});
index.on('end', function(){
res.end();
});
}
if(req.url == '/style.css'){
var style = fs.createReadStream('./style.css',{encoding: 'utf-8'});
res.writeHeader(200,
{'Content-Type': 'text/css'}
);
style.on('data', function(chunk){
res.write(chunk);
});
style.on('end', function(){
res.end();
});
}
if(req.url == '/main.js') {
var main = fs.createReadStream('./main.js',{encoding: 'utf-8'});
main.on('data', function(chunk){
if(chunk)
res.write(chunk);
});
main.on('end', function(){
res.end();
});
}
} else if(req.method == 'POST' && req.url == '/sayHi'){
res.write('<h3>Hi back to you!</h3>');
logRequest(`200 - ${req.url} - ${req.method}: Someone said Hi!\n`);
res.end();
} else if(req.method == 'POST' && req.url == '/greeting'){
let data = '';
req.on('data', function(chunk){
data += chunk;
});
req.on('end', function(){
if(data == 'Hello'){
res.write('<h3>Hello There!</h3>');
} else if(data == "what's up"){
res.write('<h3>The Sky!</h3>');
}
logRequest(`200 - ${req.url} - ${req.method}: ${data}\n`)
res.end();
});
}
}
server.listen(3000);