-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (53 loc) · 1.69 KB
/
index.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
// const math = require('./node')
// console.log(math)
/* Lecture # 6 */
const EventEmitter = require("events")
const myEmitter = new EventEmitter()
const http = require('http')
const fs = require('fs')
const url = require('url')
const path = require('path')
const pathname = path.join(__dirname, "text.txt")
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");
}
});
const myURL = url.parse(req.url);
console.log(myURL)
// Routing logic
switch (req.url) {
case '/':
const home = fs.readFileSync("./home.html", "utf-8")
res.end(home); // what we are displaying on the web-browser
console.log(home)
break;
case '/dashboard':
res.write("Dashboard");
break;
case '/contacts':
res.write("Contacts")
const user = myURL.query.name;
// const id = myURL.query.id;
console.log(user);
// console.log(id)
res.write(`This is the userName ${user}`)
myEmitter.on("FileLoaded", () => {
console.log("Contact provided")
})
myEmitter.emit("FileLoaded")
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");
});