Bird
Raised Fist0
Expressframework~20 mins

Event-driven architecture in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event-Driven Express Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Express event listener do?
Consider this Express app snippet that listens for a custom event. What will be logged when the event 'userRegistered' is emitted?
Express
const express = require('express');
const EventEmitter = require('events');
const app = express();
const eventEmitter = new EventEmitter();

eventEmitter.on('userRegistered', (user) => {
  console.log(`Welcome ${user.name}!`);
});

// Emitting event
const newUser = { name: 'Alice' };
eventEmitter.emit('userRegistered', newUser);
AWelcome Alice!
BuserRegistered event emitted
CError: eventEmitter.emit is not a function
DNo output
Attempts:
2 left
💡 Hint
Think about what happens when the 'userRegistered' event is emitted and how the listener reacts.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Express event code
Which option contains the correct syntax to listen for an event named 'dataReceived' using Node.js EventEmitter in Express?
Express
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Choose the correct listener syntax
Aemitter.listen('dataReceived', (data) => console.log(data));
Bemitter.on('dataReceived' (data) => console.log(data));
Cemitter.addListener('dataReceived', (data) => console.log(data));
Demitter.on('dataReceived', (data) => console.log(data));
Attempts:
2 left
💡 Hint
Check the method names and syntax for event listeners in Node.js EventEmitter.
❓ state_output
advanced
2:00remaining
What is the output after emitting multiple events?
Given this Express event-driven code, what will be the console output after running it?
Express
const EventEmitter = require('events');
const emitter = new EventEmitter();

let count = 0;
emitter.on('increment', () => {
  count += 1;
  console.log(`Count is now ${count}`);
});

emitter.emit('increment');
emitter.emit('increment');
emitter.emit('increment');
ANo output
B
Count is now 1
Count is now 2
Count is now 3
C
Count is now 1
Count is now 1
Count is now 1
DCount is now 3
Attempts:
2 left
💡 Hint
Each 'increment' event triggers the listener that increases count and logs it.
🔧 Debug
advanced
2:00remaining
Why does this Express event listener not respond?
This code intends to log a message when 'orderPlaced' event is emitted, but nothing happens. What is the cause?
Express
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('orderPlaced', () => {
  console.log('Order received!');
});

// Later in code
emitter.emit('orderPlaced');
emitter.emit('orderPlaced');
AEventEmitter is not imported correctly.
BThe event name is misspelled in the emit call.
CThe listener only runs once, so only the first emit triggers the log.
DThe listener should use 'on' instead of 'once' to work.
Attempts:
2 left
💡 Hint
Check how 'once' differs from 'on' in event listeners.
🧠 Conceptual
expert
2:00remaining
Which statement best describes event-driven architecture in Express?
Select the option that correctly explains the main advantage of using event-driven architecture in an Express app.
AIt allows the app to respond asynchronously to events, improving scalability and responsiveness.
BIt forces all code to run synchronously, ensuring predictable execution order.
CIt eliminates the need for middleware by handling all logic in event listeners.
DIt requires the app to restart after every event to update state.
Attempts:
2 left
💡 Hint
Think about how events help apps handle multiple tasks without waiting.

Practice

(1/5)
1. What is the main purpose of using EventEmitter in an Express app?
easy
A. To allow the app to react to events asynchronously
B. To handle HTTP routing and middleware
C. To connect to a database
D. To serve static files

Solution

  1. Step 1: Understand the role of EventEmitter

    EventEmitter is used to create and listen for events, enabling asynchronous reactions in the app.
  2. Step 2: Compare with other Express features

    Routing, database connections, and static file serving are handled by other parts of Express, not EventEmitter.
  3. Final Answer:

    To allow the app to react to events asynchronously -> Option A
  4. Quick Check:

    EventEmitter = async event handling [OK]
Hint: EventEmitter means reacting to events, not routing or DB [OK]
Common Mistakes:
  • Confusing EventEmitter with routing or middleware
  • Thinking EventEmitter manages database connections
  • Assuming EventEmitter serves static files
2. Which of the following is the correct way to create an event emitter in Express?
easy
A. const emitter = require('events').EventEmitter();
B. const EventEmitter = require('events').EventEmitter; const emitter = new EventEmitter();
C. const emitter = new require('events');
D. const EventEmitter = require('events'); const emitter = new EventEmitter();

Solution

  1. Step 1: Recall correct import syntax

    The events module exports EventEmitter as a property, so you must access it before creating an instance.
  2. Step 2: Check each option's syntax

    const EventEmitter = require('events').EventEmitter; const emitter = new EventEmitter(); correctly imports EventEmitter and creates a new instance. const EventEmitter = require('events'); const emitter = new EventEmitter(); misses the .EventEmitter property. Options A and C misuse the constructor call.
  3. Final Answer:

    const EventEmitter = require('events').EventEmitter; const emitter = new EventEmitter(); -> Option B
  4. Quick Check:

    Correct import and new instance = const EventEmitter = require('events').EventEmitter; const emitter = new EventEmitter(); [OK]
Hint: Remember to get EventEmitter from require('events').EventEmitter [OK]
Common Mistakes:
  • Calling EventEmitter as a function without new
  • Missing .EventEmitter when requiring events
  • Using new with require() directly
3. What will be the output of this Express event-driven code?
const EventEmitter = require('events').EventEmitter;
const emitter = new EventEmitter();
emitter.on('greet', (name) => {
  console.log(`Hello, ${name}!`);
});
emitter.emit('greet', 'Alice');
medium
A. Hello, Alice!
B. greet Alice
C. Error: greet event not found
D. No output

Solution

  1. Step 1: Understand event registration

    The code registers a listener for the 'greet' event that logs a greeting with the given name.
  2. Step 2: Analyze event emission

    When emitter.emit('greet', 'Alice') runs, it triggers the listener, printing "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Emit triggers listener output = Hello, Alice! [OK]
Hint: emit triggers on() listener with arguments [OK]
Common Mistakes:
  • Expecting no output without console.log
  • Confusing event name with output text
  • Thinking emit returns a value
4. Identify the error in this Express event-driven code:
const EventEmitter = require('events').EventEmitter;
const emitter = new EventEmitter();
emitter.emit('start');
emitter.on('start', () => {
  console.log('Started');
});
medium
A. emit method does not exist
B. EventEmitter is not imported correctly
C. Listener callback syntax is invalid
D. The event listener is registered after the event is emitted

Solution

  1. Step 1: Check event listener registration timing

    The listener for 'start' is added after emitter.emit('start') is called, so it misses the event.
  2. Step 2: Verify other code parts

    Import and syntax are correct; emit exists and callback syntax is valid.
  3. Final Answer:

    The event listener is registered after the event is emitted -> Option D
  4. Quick Check:

    Listener must be before emit to catch event [OK]
Hint: Register listeners before emitting events [OK]
Common Mistakes:
  • Emitting event before listener causes no output
  • Assuming emit queues events for future listeners
  • Misreading import or callback syntax
5. You want to build a real-time notification system in Express using event-driven architecture. Which approach best organizes your code for scalability?
hard
A. Create a single global EventEmitter instance and emit all events through it
B. Use only middleware functions without events for notifications
C. Create separate EventEmitter instances for different modules and communicate via events
D. Handle notifications by polling the database every second

Solution

  1. Step 1: Consider modular event handling

    Using separate EventEmitter instances per module keeps code organized and decoupled, improving scalability.
  2. Step 2: Evaluate other options

    A single global emitter can become a bottleneck. Middleware alone doesn't handle async events well. Polling is inefficient and not event-driven.
  3. Final Answer:

    Create separate EventEmitter instances for different modules and communicate via events -> Option C
  4. Quick Check:

    Modular emitters = scalable event-driven design [OK]
Hint: Use modular EventEmitters for clean, scalable code [OK]
Common Mistakes:
  • Using one global emitter causing tight coupling
  • Ignoring event-driven benefits by polling
  • Relying only on middleware for async events
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard