0
0
Node.jsframework~15 mins

EventEmitter class in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic EventEmitter Usage in Node.js
📖 Scenario: You are building a simple notification system in Node.js where different parts of your app can listen for and react to events.
🎯 Goal: Create a Node.js script that uses the EventEmitter class to emit and listen for a custom event called greet. When the event is emitted, a listener should print a greeting message.
📋 What You'll Learn
Create an EventEmitter instance
Define a listener function for the greet event
Emit the greet event with a name parameter
The listener should print a greeting message using the name
💡 Why This Matters
🌍 Real World
EventEmitters are used in Node.js to handle asynchronous events like user actions, data loading, or system signals.
💼 Career
Understanding EventEmitter is essential for backend developers working with Node.js to build responsive and event-driven applications.
Progress0 / 4 steps
1
Import EventEmitter and create an instance
Write a line to import EventEmitter from the events module and create a constant called emitter as a new EventEmitter instance.
Node.js
Need a hint?

Use require('events') to import EventEmitter and then create a new instance with new EventEmitter().

2
Add a listener for the 'greet' event
Add a listener to emitter for the event named 'greet' using emitter.on. The listener should be a function that takes a parameter name.
Node.js
Need a hint?

Use emitter.on('greet', (name) => { ... }) to listen for the greet event.

3
Print a greeting message inside the listener
Inside the listener function for 'greet', write a line to print Hello, {name}! using console.log and the name parameter.
Node.js
Need a hint?

Use a template string inside console.log to include the name variable.

4
Emit the 'greet' event with a name
Emit the 'greet' event on emitter with the argument 'Alice' to trigger the listener.
Node.js
Need a hint?

Use emitter.emit('greet', 'Alice') to send the event with the name.