-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathevents.js
46 lines (34 loc) · 884 Bytes
/
events.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
const EventEmitter = require("events");
const http = require("http");
class Sales extends EventEmitter {
constructor() {
super();
}
}
const myEmitter = new Sales();
myEmitter.on("newSale", () => {
console.log("There was a new sale!");
});
myEmitter.on("newSale", () => {
console.log("Costumer name: Jonas");
});
myEmitter.on("newSale", stock => {
console.log(`There are now ${stock} items left in stock.`);
});
myEmitter.emit("newSale", 9);
//////////////////
const server = http.createServer();
server.on("request", (req, res) => {
console.log("Request received!");
console.log(req.url);
res.end("Request received");
});
server.on("request", (req, res) => {
console.log("Another request 😀");
});
server.on("close", () => {
console.log("Server closed");
});
server.listen(8000, "127.0.0.1", () => {
console.log("Waiting for requests...");
});