0
0
Expressframework~10 mins

Handling connection events in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to listen for new client connections on the server.

Express
const express = require('express');
const app = express();
const server = app.listen(3000);
server.on('[1]', (socket) => {
  console.log('A client connected');
});
Drag options to blanks, or click blank then click option'
Aconnecting
Bconnected
Cconnection
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'connection' causes the event not to fire.
2fill in blank
medium

Complete the code to handle when a client disconnects.

Express
server.on('connection', (socket) => {
  socket.on('[1]', () => {
    console.log('Client disconnected');
  });
});
Drag options to blanks, or click blank then click option'
Adisconnect
Bclose
Cend
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'close' will not catch client disconnects.
3fill in blank
hard

Fix the error in the code to properly handle data received from a client.

Express
server.on('connection', (socket) => {
  socket.on('[1]', (data) => {
    console.log('Received:', data.toString());
  });
});
Drag options to blanks, or click blank then click option'
Adata
Binput
Cmessage
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' causes no data event to be caught on a raw socket.
4fill in blank
hard

Fill both blanks to create a server that logs when a client connects and disconnects.

Express
const net = require('net');
const server = net.createServer((socket) => {
  console.log('Client [1]');
  socket.on('[2]', () => {
    console.log('Client disconnected');
  });
});
server.listen(8080);
Drag options to blanks, or click blank then click option'
Aconnected
Bconnect
Cclose
Dconnection
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' event on socket instead of 'close' for disconnection.
5fill in blank
hard

Fill all three blanks to handle connection, data reception, and disconnection events properly.

Express
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);
Drag options to blanks, or click blank then click option'
Aconnection
Bdata
Cdisconnect
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'close' for client disconnection event.