Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the built-in 'fs' module in Node.js.
Node.js
const fs = require([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using browser-specific modules like 'express' instead of 'fs'.
Forgetting to put quotes around the module name.
✗ Incorrect
Node.js uses require to import built-in modules like fs for file system operations.
2fill in blank
mediumComplete the code to create a simple HTTP server in Node.js.
Node.js
const http = require('http'); 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 methods like 'start' or 'run' which do not exist on the server object.
Forgetting to call the method with parentheses.
✗ Incorrect
The listen method starts the server on the specified port.
3fill in blank
hardFix the error in the code to read a file asynchronously in Node.js.
Node.js
const fs = require('fs'); fs.readFile('example.txt', 'utf8', (err, data) => { if (err) throw err; console.log([1]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Logging the error object instead of the file content.
Trying to log the module or file name instead of the data.
✗ Incorrect
The callback receives the file content as data, which should be logged.
4fill in blank
hardFill both blanks to export a function named 'greet' in Node.js.
Node.js
function greet() {
console.log('Hello!');
}
module.[1] = [2]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'require' instead of 'exports' for exporting.
Assigning the wrong variable to exports.
✗ Incorrect
Use module.exports to export the greet function so other files can use it.
5fill in blank
hardFill all three blanks to create a simple Express server that responds with 'Hi' on the root path.
Node.js
const express = require([1]); const app = express(); app.[2]('/', (req, res) => { res.[3]('Hi'); }); app.listen(3000);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the module name.
Using 'post' instead of 'get' for the route method.
Using 'write' instead of 'send' to respond.
✗ Incorrect
Import Express with 'express', use get to handle GET requests, and send to send the response.