0
0
Expressframework~10 mins

Event-driven architecture in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event-driven architecture
Event Occurs
Event Emitted
Event Listeners Check
Listener 1
Listener 1 Action
Continue Program
When an event happens, it is emitted. Listeners waiting for that event react by running their actions.
Execution Sample
Express
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.on('greet', () => {
  console.log('Hello!');
});

emitter.emit('greet');
This code sets up a listener for a 'greet' event and then emits that event, causing the listener to run.
Execution Table
StepActionEventListeners PresentListener ActionOutput
1Create EventEmitter instanceN/ANone yetNoneNo output
2Add listener for 'greet''greet'1 listener addedListener waitsNo output
3Emit 'greet' event'greet'1 listener activeRun listener callbackPrints 'Hello!'
4No more eventsN/AListeners remainNo actionExecution ends
💡 No more events emitted, program stops listening.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
emitterundefinedEventEmitter instanceSame instance with listenerSame instanceSame instance
listeners for 'greet'001 listener function1 listener function1 listener function
Key Moments - 2 Insights
Why does the listener run only after emit is called?
Listeners wait silently until the event is emitted (see Step 3 in execution_table). Emitting triggers all listeners for that event.
Can multiple listeners respond to the same event?
Yes, multiple listeners can be added and all run when the event is emitted (concept_flow shows branching to multiple listeners).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 3?
APrints 'Hello!'
BNo output
CError thrown
DListener removed
💡 Hint
Check the 'Output' column at Step 3 in execution_table.
At which step is the listener added for the 'greet' event?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Listeners Present' columns in execution_table.
If we emit 'greet' twice, how would the output change?
APrints 'Hello!' once
BPrints 'Hello!' twice
CNo output
DError on second emit
💡 Hint
Each emit triggers all listeners; see Step 3 behavior in execution_table.
Concept Snapshot
Event-driven architecture in Express:
- Create an EventEmitter instance
- Add listeners with .on(event, callback)
- Emit events with .emit(event)
- All listeners for that event run on emit
- Allows decoupled, asynchronous reactions to events
Full Transcript
Event-driven architecture means your program waits for events to happen, then reacts. In Express, you create an EventEmitter object. You add listeners that wait for named events. When you emit an event, all listeners for that event run their code. This lets your app respond to things like user actions or data arriving without blocking other work. The example code shows adding a listener for 'greet' and then emitting 'greet' to print 'Hello!'. Listeners only run when their event is emitted. You can have many listeners for the same event. Emitting multiple times runs listeners each time.