0
0
Expressframework~20 mins

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

Choose your learning style9 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.