-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverCreation.js
36 lines (29 loc) · 888 Bytes
/
serverCreation.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
const http = require('http');
const fs = require('fs');
function dataControl(req, res) {
console.log("Server is running");
// Log the request details to text.txt
const log = `\n ${new Date().getHours().toString()}\t ${req.url} \t New request received`;
fs.appendFile("text.txt", log, (err) => {
if (err) {
console.error("Failed to write to file");
}
});
// Routing logic
switch (req.url) {
case '/':
res.write("HomePage"); // what we are displaying on the web-browser
break;
case '/dashboard':
res.write("Dashboard");
break;
default:
res.write("Nodejs Server Running");
break;
}
res.end();
}
// Create the server
http.createServer(dataControl).listen(3000, () => {
console.log("Server is listening on port 3000");
});