forked from balwantjassal/practical-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventDemo.js
63 lines (59 loc) · 1.87 KB
/
eventDemo.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
var events=require("events");
var http=require("http");
var querystring = require('querystring');
var server=http.createServer(function(req,res){
debugger;
if(req.url=="/"){
if(req.method=="POST"){
console.log("Data Posted");
var body="";
// res.end("<h1>Post Successfull...</h1>"+req.body);
}else{
res.writeHead(200,{'content-type':'text/html'});
var str="<h1>My Form</h1>";
str+='<form method="post" action="/">';
str+='Enter Name:<input type="text" name="uname" id="uname" value=""/><br/><br/>';
str+='<input type="submit" value="Submit Data"/></form>';
res.write(str);
res.end();
}
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = querystring.parse(body);
console.log(post);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
// eventEmitter.emit("HomePage","Sample Data");
}
if(req.url=="/student"){
res.write("This is STUDENT Web page");
res.end();
//eventEmitter.emit("StudentEvent");
}
if(req.url=="/employee"){
res.write("This is Employee Web page");
res.end();
}
});
server.listen(5000);
console.log("Server Connected at Port 5000");
// Event object Creation
var eventEmitter=new events.EventEmitter();
//Event handler....
var eventHandler=function(){
console.log("I'm Ready to handle an EVENT");
};
// Binding Event Name with Event handler
eventEmitter.on("connection",eventHandler);
// Emitting an Event named as connection
eventEmitter.emit("connection");
eventEmitter.on("HomePage",(data)=>{
console.log("Home Page Reflected " + data);
});
var studentHandler=function(){
console.log("Student Page Reflected ");
};
eventEmitter.on("StudentEvent",studentHandler);