0
0
Expressframework~10 mins

Why real-time matters in Express - Test Your Understanding

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

Complete the code to create a basic Express server that listens on port 3000.

Express
const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});
app.listen([1], () => {
  console.log('Server running');
});
Drag options to blanks, or click blank then click option'
A3000
B8080
C5000
D4000
Attempts:
3 left
💡 Hint
Common Mistakes
Using a port number that is already in use or not standard for local development.
2fill in blank
medium

Complete the code to import and use the Socket.IO library for real-time communication.

Express
const http = require('http');
const express = require('express');
const app = express();
const server = http.createServer(app);
const io = require('[1]')(server);

io.on('connection', (socket) => {
  console.log('A user connected');
});
Drag options to blanks, or click blank then click option'
Asocket.io
Bexpress
Chttp
Dws
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' or 'http' instead of the real-time library name.
3fill in blank
hard

Fix the error in the code to emit a message to the connected client.

Express
io.on('connection', (socket) => {
  socket.[1]('message', 'Hello everyone!');
});
Drag options to blanks, or click blank then click option'
Aon
Bsend
Cbroadcast
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' which is not a Socket.IO method for events.
Using 'broadcast' incorrectly.
4fill in blank
hard

Fill both blanks to broadcast a message to all clients except the sender.

Express
io.on('connection', (socket) => {
  socket.[1].[2]('message', 'Hello others!');
});
Drag options to blanks, or click blank then click option'
Abroadcast
Bemit
Csend
Don
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit'.
Not using 'broadcast' to exclude the sender.
5fill in blank
hard

Fill all three blanks to set up a server that listens on port 4000 and sends a welcome message on connection.

Express
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const io = require('[1]')(server);

io.on('connection', ([2]) => {
  [2].emit('welcome', 'Welcome to the real-time server!');
});

server.listen([3], () => {
  console.log('Server listening on port 4000');
});
Drag options to blanks, or click blank then click option'
Asocket.io
Bsocket
C4000
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'socket.io' for the first blank.
Using wrong variable name for the connection.
Wrong port number.