Challenge - 5 Problems
Connection Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a client connects to this Express server?
Consider this Express server code. What will be logged when a client connects to the server?
Express
const express = require('express'); const app = express(); const server = app.listen(3000, () => console.log('Server started')); server.on('connection', (socket) => { console.log('New client connected'); });
Attempts:
2 left
💡 Hint
The 'connection' event is emitted by the underlying HTTP server when a new TCP connection is made.
✗ Incorrect
The 'connection' event on the server object is triggered each time a client connects. The callback logs 'New client connected'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Express connection event handler
Which option correctly fixes the syntax error in this code snippet?
Express
const express = require('express'); const app = express(); const server = app.listen(3000); server.on('connection' (socket) => { console.log('Client connected'); });
Attempts:
2 left
💡 Hint
Check the syntax of the event listener function parameters.
✗ Incorrect
The event name and callback function must be separated by a comma. Missing comma causes syntax error.
🔧 Debug
advanced2:00remaining
Why does this Express server not log connection events?
This code is intended to log when a client connects, but nothing is logged. What is the cause?
Express
const express = require('express'); const app = express(); app.on('connection', (socket) => { console.log('Client connected'); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Consider which object emits the 'connection' event in Node.js HTTP servers.
✗ Incorrect
The Express app object does not emit 'connection' events. The underlying HTTP server returned by app.listen does.
❓ state_output
advanced2:00remaining
What is the value of 'activeConnections' after three clients connect and one disconnects?
This Express server tracks active connections. What is the value of 'activeConnections' after three clients connect and one disconnects?
Express
const express = require('express'); const app = express(); const server = app.listen(3000); let activeConnections = 0; server.on('connection', (socket) => { activeConnections++; socket.on('close', () => { activeConnections--; }); }); // Assume three clients connect, then one disconnects
Attempts:
2 left
💡 Hint
Each connection increments, each close decrements the counter.
✗ Incorrect
Three connections increase activeConnections to 3, one disconnect decreases it to 2.
🧠 Conceptual
expert2:00remaining
Which event should you listen to for detecting when an HTTP request is fully received in Express?
You want to run code when the entire HTTP request body has been received by your Express server. Which event is the best to listen for?
Attempts:
2 left
💡 Hint
Think about the event that signals the end of data streaming on a request.
✗ Incorrect
The 'end' event on the request object fires when the full request body has been received.