Complete the code to import the HTTP module in Node.js.
const http = require([1]);Node.js uses the require function to import built-in modules like http for creating servers.
Complete the code to create a simple server that listens on port 3000.
http.createServer((req, res) => {
res.end('Hello World');
}).[1](3000);The listen method tells the server to start listening on a specific port.
Fix the error in the code to correctly handle asynchronous file reading in Node.js.
const fs = require('fs'); fs.readFile('file.txt', [1], (err, data) => { if (err) throw err; console.log(data.toString()); });
toString() without it.Passing 'utf8' as the encoding makes readFile return a string instead of a buffer.
Fill both blanks to create a simple Express server that responds with 'Hi!' on the root URL.
const express = require('express'); const app = express(); app.[1]('/', (req, res) => { res.[2]('Hi!'); }); app.listen(3000);
Use get to handle GET requests and send to send a response in Express.
Fill all three blanks to create a Node.js server that reads a file and sends its content as a response.
const http = require('http'); const fs = require('fs'); http.createServer((req, res) => { fs.readFile([1], [2], (err, data) => { if (err) { res.statusCode = 500; res.end('Error'); return; } res.setHeader('Content-Type', [3]); res.end(data); }); }).listen(8080);
The server reads 'index.html' as a UTF-8 text file and sets the content type to 'text/html' before sending the response.