-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·48 lines (33 loc) · 1.11 KB
/
main.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
console.log("Program Starting");
var http = require("http");
var events = require("events");
var eventEmitter = new events.EventEmitter();
var listener = function listener(){
console.log('listener executed');
}
eventEmitter.addListener('connection', listener);
//Create an event handler
var connectHandler = function connected(){
console.log('connection succesful.');
//fire event
eventEmitter.emit('data_received');
}
//bind the connection event with the handler
eventEmitter.on('connection',connectHandler);
//bind the data receive event with the anonymous function
eventEmitter.on('data_received',function(){
console.log('data recieved\n');
});
eventEmitter.emit('connection');
eventEmitter.removeListener('connection',listener);
eventEmitter.emit('connection');
console.log("Program ended");
http.createServer(function(request, response){
//send http header
//status 200 : OK
//content type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
//send the response body as "whats good"
response.end('whats good\n');
}).listen(8081);
console.log('server running at http://127.0.0.1:8081/');