-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (39 loc) · 1.44 KB
/
Copy pathserver.js
File metadata and controls
49 lines (39 loc) · 1.44 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
// nodejs server logic code goes here
const http = require('http');
// file system module to perform file operations
const fs = require('fs');
// import file with logic for generating os data
const os = require('./getOsData')
//declare host and port
const host = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
const urlPath = req.url;
if (urlPath === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
html = fs.readFileSync('./pages/index.html');
res.end(html);
} else if (urlPath === '/about') {
res.writeHead(200, {'Content-Type': 'text/html'});
html = fs.readFileSync('./pages/about.html');
res.end(html);
} else if (urlPath === '/sys') {
res.writeHead(201, {'Content-Type': 'text/plain'});
// stringify JSON Object
let jsonContent = JSON.stringify(os.jsonData, null, 2);
fs.writeFile("osinfo.json", jsonContent, 'utf8', function(err) {
if (err) {
return console.log("An error occured", err);
}
console.log("JSON file has been saved.");
res.end('Your OS info has been saved successfully!');
});
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
html = fs.readFileSync('./pages/404.html');
res.end(html);
}
});
server.listen(port, host, () => {
console.log(`Server running at ${host}:${port}`);
});