Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the HTTP module.
Node.js
const http = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' or 'path' instead of 'http' to import the HTTP module.
✗ Incorrect
The HTTP module is imported using require('http') to create a server.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' instead of 'createServer' to create the server.
✗ Incorrect
The createServer method creates a new HTTP server that handles requests.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'run' instead of 'listen' to start the server.
✗ Incorrect
The listen method starts the server and listens on the specified port.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using status code 404 or content type 'application/json' for plain text response.
✗ Incorrect
Status code 200 means OK, and 'text/plain' sets the response type to plain text.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name, status code, or content type for JSON response.
✗ Incorrect
The server is created with createServer, status code 200 means OK, and content type 'application/json' tells the browser to expect JSON data.