0
0
Node.jsframework~10 mins

How Node.js differs from browser JavaScript 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 built-in 'fs' module in Node.js.

Node.js
const fs = require([1]);
Drag options to blanks, or click blank then click option'
A'http'
B'fs'
C'express'
D'path'
Attempts:
3 left
💡 Hint
Common Mistakes
Using browser-specific modules like 'express' instead of 'fs'.
Forgetting to put quotes around the module name.
2fill in blank
medium

Complete 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'
Alisten
Bstart
Copen
Drun
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.
3fill in blank
hard

Fix 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'
Aexample.txt
Berr
Cfs
Ddata
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.
4fill in blank
hard

Fill 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'
Aexports
Bgreet
Cmodule
Drequire
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'require' instead of 'exports' for exporting.
Assigning the wrong variable to exports.
5fill in blank
hard

Fill 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'
A'express'
Bget
Csend
D'http'
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.