Challenge - 5 Problems
Event-Driven Express Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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);
Attempts:
2 left
💡 Hint
Think about what happens when the 'userRegistered' event is emitted and how the listener reacts.
✗ Incorrect
The event listener listens for 'userRegistered' and logs a welcome message with the user's name when the event is emitted.
📝 Syntax
intermediate2: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 syntaxAttempts:
2 left
💡 Hint
Check the method names and syntax for event listeners in Node.js EventEmitter.
✗ Incorrect
The correct method to listen for events is 'on'. Option D uses a non-existent method 'listen'. Option D is valid Node.js syntax but not the best choice here since only one correct answer is allowed. Option D misses a comma causing syntax error.
❓ state_output
advanced2: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');
Attempts:
2 left
💡 Hint
Each 'increment' event triggers the listener that increases count and logs it.
✗ Incorrect
Each time 'increment' is emitted, the listener runs, increasing count by 1 and logging the new value. So three emits produce three logs with increasing count.
🔧 Debug
advanced2: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');
Attempts:
2 left
💡 Hint
Check how 'once' differs from 'on' in event listeners.
✗ Incorrect
'once' listens for the event only one time. The first emit triggers the listener and logs the message. The second emit does nothing because the listener was removed after first call.
🧠 Conceptual
expert2: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.
Attempts:
2 left
💡 Hint
Think about how events help apps handle multiple tasks without waiting.
✗ Incorrect
Event-driven architecture lets the app react to events as they happen, often asynchronously, which helps handle many tasks efficiently without blocking.