Skip to content

Commit

Permalink
event and EventEmitter in node js
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Aug 1, 2022
1 parent 58e7902 commit ebadd1e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//counting how many times the API is called using EventEmmiter

const express = require('express');
const EventEmitter = require('events');
const app = express();
const event = new EventEmitter();
let count =0;


app.get('/' , (req , res)=>{
res.send('api called');
event.emit("countAPI"); //generating/triggering an event having name "countAPI"
});

app.get('/search' , (req , res)=>{
res.send('search api called');
event.emit("countAPI");
});

app.get('/update' , (req , res)=>{
res.send('update api called');
event.emit("countAPI");
});

//handelling the generated event
event.on("countAPI" , ()=>{
count++;
console.log("event called" , count);
})


app.listen(8000);

0 comments on commit ebadd1e

Please sign in to comment.