Complete the code to listen for new client connections on the server.
const express = require('express'); const app = express(); const server = app.listen(3000); server.on('[1]', (socket) => { console.log('A client connected'); });
The 'connection' event is emitted when a new client connects to the server.
Complete the code to handle when a client disconnects.
server.on('connection', (socket) => { socket.on('[1]', () => { console.log('Client disconnected'); }); });
The 'close' event is fired when a client disconnects from the server socket.
Fix the error in the code to properly handle data received from a client.
server.on('connection', (socket) => { socket.on('[1]', (data) => { console.log('Received:', data.toString()); }); });
The 'data' event is emitted when the socket receives data from the client.
Fill both blanks to create a server that logs when a client connects and disconnects.
const net = require('net'); const server = net.createServer((socket) => { console.log('Client [1]'); socket.on('[2]', () => { console.log('Client disconnected'); }); }); server.listen(8080);
'connected' is a message to log, and 'close' is the event to listen for client disconnection.
Fill all three blanks to handle connection, data reception, and disconnection events properly.
const net = require('net'); const server = net.createServer(); server.on('[1]', (socket) => { console.log('Client connected'); socket.on('[2]', (data) => { console.log('Data:', data.toString()); }); socket.on('[3]', () => { console.log('Client disconnected'); }); }); server.listen(9000);
'connection' event fires when a client connects, 'data' when data arrives, and 'close' when client leaves.