0
0
Node.jsframework~10 mins

Creating a basic HTTP server in Node.js - Interactive Practice

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

Complete the code to import the HTTP module.

Node.js
const http = require('[1]');
Drag options to blanks, or click blank then click option'
Aurl
Bfs
Chttp
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' or 'path' instead of 'http' to import the HTTP module.
2fill in blank
medium

Complete the code to create an HTTP server.

Node.js
const server = http.[1]((req, res) => {
  res.end('Hello World');
});
Drag options to blanks, or click blank then click option'
AcreateServer
Brequest
Clisten
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' instead of 'createServer' to create the server.
3fill in blank
hard

Fix the error in the code to start the server on port 3000.

Node.js
server.[1](3000, () => {
  console.log('Server running on port 3000');
});
Drag options to blanks, or click blank then click option'
Alisten
Bstart
Copen
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'run' instead of 'listen' to start the server.
4fill in blank
hard

Fill both blanks to send a plain text response with status 200.

Node.js
res.statusCode = [1];
res.setHeader('Content-Type', [2]);
res.end('Hello');
Drag options to blanks, or click blank then click option'
A200
B'text/plain'
C'application/json'
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using status code 404 or content type 'application/json' for plain text response.
5fill in blank
hard

Fill all three blanks to create a server that responds with JSON and listens on port 8080.

Node.js
const server = http.[1]((req, res) => {
  res.statusCode = [2];
  res.setHeader('Content-Type', [3]);
  res.end(JSON.stringify({ message: 'Hi' }));
});
server.listen(8080);
Drag options to blanks, or click blank then click option'
AcreateServer
B200
C'application/json'
D'text/html'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name, status code, or content type for JSON response.