-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (64 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Importing modules http, os, fs
const http = require('http');
const os = require('os');
const fs = require('fs');
//Initializing port
const port = 4000;
let osinfo = {
hostname: os.hostname(),
platform: os.platform(),
architecture: os.arch(),
numberOfCPUS: os.cpus().length,
networkInterfaces: os.networkInterfaces(),
uptime: os.uptime(),
};
// console.log(osinfo);
const osinfoData = JSON.stringify(osinfo);
// console.log(osinfoData);
//defining server
const server = http.createServer((req,res) => {
const url = req.url;
if(url === '/'){
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.readFile('pages/index.html', (err,data) => {
if(err){
res.statusCode = 404;
res.write('File not found');
} else{
res.write(data);
}
res.end();
});
} else if(url === '/about'){
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.readFile('pages/about.html', (err,data) => {
res.write(data);
res.end();
});
} else if (url === '/sys'){
res.statusCode = 201;
res.setHeader('Content-Type', 'text/plain');
fs.writeFile('osinfo.json', osinfoData, 'utf8', (err) => {
if (err) {
console.log(err);
} else {
// console.log('osinjo.json is saved successfully');
res.write('Your OS info has been saved successfully');
res.end();
}
});
}else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
fs.readFile('pages/404.html', (err,data) => {
res.write(data);
res.end();
});
}
});
// Starting server
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});