FREE COURSE | BEGINNER
Node.js Fundamentals
Build server-side JavaScript applications using Node.js.
Learning objectives
- Explain the runtime and use modules and npm.
- Read files and reason about asynchronous execution.
- Create a local HTTP server and JSON API.
- Predict and test request outcomes.
Prerequisite: Basic JavaScript. Install Node.js locally to execute the editable examples. Official Node.js learning resources.
01. Introduction to Node.js
Node.js is a JavaScript runtime, not a programming language or framework. Unlike browser JavaScript, it provides APIs for files, processes and networking. It is commonly used for servers, command-line tools and automation. Browser APIs such as document are not normally available.
Editable Node.js example
Original example output
Reveal expected output
Hello from Node.js undefined
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Explain why document is undefined in Node.js.
02. Installation and Setup
Install a supported LTS release from nodejs.org. Open a terminal and check node -v and npm -v. Save the example as hello.cjs and run node hello.cjs in its directory. The .cjs extension explicitly selects CommonJS modules.
Editable Node.js example
Original example output
Reveal expected output
Welcome, Alex!
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Change the learner name and run the file again.
03. Modules and npm
Modules organize reusable code. CommonJS uses require; ES modules use import. The node: prefix identifies built-in modules. npm manages external packages and package.json records project metadata and dependencies. Run npm init -y in a new project to create package.json. This example needs no external packages.
Editable Node.js example
Original example output
Reveal expected output
.html
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Change the filename to report.pdf and predict the result.
04. File System Operations
The node:fs module reads and writes files. Relative paths resolve from the working directory. Use a new practice directory: writeFileSync replaces an existing file. Synchronous operations block execution; asynchronous APIs are preferred for many server tasks.
Editable Node.js example
Original example output
Reveal expected output
Learn Node.js
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Change the text in a disposable practice directory and inspect the file.
05. Asynchronous Programming
Node.js can handle waiting operations without blocking the main JavaScript thread. Promises represent eventual completion; await pauses the surrounding async function, not the entire process. Handle rejected promises with catch or try/catch. CPU-heavy synchronous work can still block the event loop.
Editable Node.js example
Original example output
Reveal expected output
Start Other work Finished
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Predict the order before running. Explain why Other work appears before Finished.
06. Creating an HTTP Server
An HTTP server receives requests and returns responses. The built-in node:http module can create a server without installing a framework. Bind to localhost for practice. Run the file and visit http://127.0.0.1:3000 in your browser; stop the server with Ctrl+C.
Editable Node.js example
Original example output
Reveal expected output
Browser response: Hello from the server!
Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Change the response message and restart the server.
07. JSON and REST APIs
JSON is a text format for exchanging structured data. JSON.stringify converts JavaScript values into JSON text. REST-style APIs commonly use HTTP methods such as GET to retrieve resources and POST to create them. Status 200 means success; 404 means not found; 405 means method not allowed.
Editable Node.js example
Original example output
Reveal expected output
{"id":1,"title":"Node.js"}Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Add a level property and inspect the JSON output.
08. Interactive Practice Lab
Explore how an API responds to different request methods and paths. The browser simulator below uses fixed sample data and does not execute edited Node.js code or contact a server. Compare the result with your prediction.
Editable Node.js example
Original example output
Reveal expected output
[{"id":1,"title":"Node.js"}]Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Try GET /courses, GET /missing and POST /courses. Explain the status codes.
API request simulator
09. Mini Project: Learning Resource API
Build a read-only API that returns a course list as JSON. Use an explicit route and method check. Save as api.cjs, run node api.cjs, and visit http://127.0.0.1:3000/courses. Stop other practice servers first. This is a local learning example, not a production API.
Editable Node.js example
Original example output
Reveal expected output
GET /courses → 200
[{"id":1,"title":"Node.js"}]Run edited code in your local Node.js terminal. This output is for the original example only.
Try it yourself
Add another course and test a missing route. For production, learn validation, authentication, logging and rate limiting.