CodingNic

Core Node.js Modules

Exercises

Core Node.js Modules 30 min read

Exercises

Objectives

By the end of this lesson, you should be able to:

  • Combine several core Node.js modules in one small program
  • Build and test a raw HTTP server without a framework
  • Explain why each core module exists and what problem it solves

⚠️ A note on verification: every command and output in this lesson was actually run with Node.js.

Exercise 1: File-Backed Note Log

a) Using fs/promises, write an async function addNote(text) that appends text (followed by a newline) to a file notes-log.txt, creating the file if it doesn’t exist yet.

b) Write readNotes(), an async function returning an array of every line in notes-log.txt (skip empty lines).

c) Call addNote() three times with different text, then call readNotes() and log the array. Expected shape:

text
[ 'First note', 'Second note', 'Third note' ]

d) Use the path module to build the file path as path.join(__dirname, 'notes-log.txt') instead of a bare string, and explain why this is safer than a hardcoded relative path once a program is run from a different working directory.

Exercise 2: A System Info Script

a) Write a script that logs the machine’s platform, architecture, CPU core count, and total memory in GB (rounded to one decimal place), using os.

b) Extend it to also log process.version (the Node.js version) and process.uptime() (seconds the current process has been running).

c) Have the script set process.exitCode = 0 at the end, and confirm with echo $? in the shell that the process exited successfully.

Exercise 3: Password Hashing Utility

a) Write hashPassword(password) and verifyPassword(password, stored) functions using crypto.scryptSync with a random salt, following the salt:hash pattern from Lesson 4.

b) Test it: hash a password, then verify both the correct password and two different incorrect passwords, confirming only the correct one returns true.

c) Add a crypto.randomUUID()-generated ID alongside each hashed password, simulating what a simple user record might look like: { id, passwordHash: 'salt:hash' }.

Exercise 4: An Order Events System

a) Create a class OrderSystem extends EventEmitter with a method placeOrder(id, total) that emits an 'order-placed' event with both arguments.

b) Register two separate listeners for 'order-placed', one that logs a confirmation message, one that logs it to a running total (keep a variable outside the class tracking the sum of every order’s total).

c) Add a listener using .once() for a 'first-order' event, emitted only the first time placeOrder is ever called, and confirm it doesn’t fire on the second or third order.

Exercise 5: A Small REST-ish HTTP Server

a) Build an http.createServer server with three routes: GET /notes (returns the notes array from Exercise 1 as JSON), GET /system (returns the system info object from Exercise 2 as JSON), and anything else returning a 404 with a JSON error body like { "error": "Not Found" }.

b) Start the server and test all three cases with curl -i, confirming the status code and Content-Type: application/json header on every response, including the 404.

c) Parse the request’s URL with the url module inside the handler (even though these routes don’t use query parameters yet), and log req.method, pathname, and every query parameter for each incoming request.

Exercise 6: Streaming a Large File

a) Generate a text file of at least 2000 characters (repeating a line is fine).

b) Copy it to a second file using .pipe(), and confirm both files have identical content afterward.

c) Separately, read the same file with readline, and log only lines containing the letter 'e' (case-insensitive).

Recap

This module covered Node’s core built-in modules: fs for files, path for building paths correctly, os and process for machine and program information, crypto for hashing and secure randomness, events for the EventEmitter pattern underlying much of Node, http for a framework-free server, url for parsing requests, and stream/readline for handling data as it arrives rather than all at once.

Next module: Express.js, the framework that wraps everything from this module into a much friendlier API.