0
0
Node.jsframework~10 mins

Why real-time matters in Node.js - 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 simple Node.js server that listens on port 3000.

Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello World');
});
server.listen([1]);
Drag options to blanks, or click blank then click option'
A3000
B8080
C5000
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using port 80 without proper permissions.
Choosing a port number that is already in use.
2fill in blank
medium

Complete the code to import the 'ws' library for WebSocket support.

Node.js
const WebSocket = [1]('ws');
Drag options to blanks, or click blank then click option'
Aimport
Bfetch
Crequire
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'import' without enabling ES modules.
Using 'fetch' which is for HTTP requests.
3fill in blank
hard

Fix the error in the WebSocket server code by completing the missing method.

Node.js
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
  ws.[1]('Welcome to the server!');
});
Drag options to blanks, or click blank then click option'
Awrite
Bsend
Cemit
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' which is for streams.
Using 'emit' which triggers events.
4fill in blank
hard

Fill both blanks to broadcast a message to all connected clients.

Node.js
wss.clients.forEach(function each([1]) {
  if ([2].readyState === WebSocket.OPEN) {
    [2].send('Broadcast message');
  }
});
Drag options to blanks, or click blank then click option'
Aclient
Bws
Csocket
Dconnection
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using undefined variable names.
5fill in blank
hard

Fill all three blanks to set up a real-time message event handler.

Node.js
wss.on('connection', function connection(ws) {
  ws.on('[1]', function incoming([2]) {
    console.log('Received: %s', [3]);
  });
});
Drag options to blanks, or click blank then click option'
Amessage
Bmsg
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'message' event.
Using different names for the parameter and logged variable.