0
0
Node.jsframework~10 mins

Why building HTTP servers 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 basic HTTP server in Node.js.

Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end([1]);
});
server.listen(3000, () => {
  console.log('Server running on port 3000');
});
Drag options to blanks, or click blank then click option'
A'Hello World!'
B'Goodbye!'
C'Error!'
D'Welcome!'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect response text
Forgetting to end the response
2fill in blank
medium

Complete the code to listen on port 8080 instead of 3000.

Node.js
server.listen([1], () => {
  console.log('Server running on port 8080');
});
Drag options to blanks, or click blank then click option'
A8080
B3000
C80
D5000
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the port as 3000
Using a port number below 1024 without permissions
3fill in blank
hard

Fix the error in the code to properly import the HTTP module using modern ES modules syntax.

Node.js
import [1] from 'http';

const server = http.createServer((req, res) => {
  res.end('Hi');
});
Drag options to blanks, or click blank then click option'
Ahttp
B{ http }
CHttp
D* as http
Attempts:
3 left
💡 Hint
Common Mistakes
Using default import syntax which is invalid for 'http' module
Using incorrect casing
4fill in blank
hard

Fill both blanks to send a JSON response with status code 200.

Node.js
const server = http.createServer((req, res) => {
  res.statusCode = [1];
  res.setHeader('Content-Type', [2]);
  res.end(JSON.stringify({ message: 'Success' }));
});
Drag options to blanks, or click blank then click option'
A200
B'application/json'
C'text/html'
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status code
Setting wrong Content-Type header
5fill in blank
hard

Fill all three blanks to create a server that responds with HTML content on port 5000.

Node.js
const server = http.createServer((req, res) => {
  res.statusCode = [1];
  res.setHeader('Content-Type', [2]);
  res.end([3]);
});
server.listen(5000, () => {
  console.log('Server running on port 5000');
});
Drag options to blanks, or click blank then click option'
A200
B'text/html'
C'<h1>Welcome!</h1>'
D'application/json'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status code
Wrong Content-Type header
Sending plain text instead of HTML