Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the built-in HTTP module in Node.js.
Node.js
const http = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' which is for file system operations.
Using 'express' which is a third-party framework, not built-in.
✗ Incorrect
The built-in HTTP module in Node.js is imported using require("http").
2fill in blank
mediumComplete the code to create a simple HTTP server that listens on port 3000.
Node.js
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World');
}).[1](3000); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' which is not a method of the server object.
Using 'run' which is not valid here.
✗ Incorrect
The method to start the server listening on a port is listen.
3fill in blank
hardFix the error in the code to correctly import the 'fs' module.
Node.js
const fs = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'file-system' which is not a built-in module name.
Using camelCase or other variations.
✗ Incorrect
The correct module name for file system operations in Node.js is "fs".
4fill in blank
hardFill both blanks to create a server that responds with JSON data.
Node.js
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': [1] });
res.end(JSON.stringify({ message: [2] }));
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/plain' as content type when sending JSON.
Putting the content type value without quotes.
✗ Incorrect
The content type for JSON data is "application/json". The message string can be any text, here 'Hello'.
5fill in blank
hardFill all three blanks to export a function named 'startServer' from a module.
Node.js
function [1]() { console.log('Server started'); } module.exports = [2] = [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for function and export.
Using camelCase incorrectly.
✗ Incorrect
The function name and the exported name should match and be 'startServer'.