Master Node.js From Scratch

Clear, interactive, and structured Node.js lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to Node.js
Introduction to Node.js What Is Node.js? Node.js Architecture Features of Node.js Node.js vs Browser JavaScript Node.js Use Cases
2. Installation and Setup
Installation Introduction LTS and Current Versions npm Setup Environment Setup Verify Installation Node.js REPL
3. Node.js Fundamentals
Fundamentals Introduction Running JavaScript Global Objects Process Object Command-Line Arguments Environment Variables
4. JavaScript Basics
JavaScript Basics Introduction Variables: var, let and const JavaScript Data Types Comparison and Logical Operators If, Else If, Else and Switch Loops and Iteration Functions and Arrow Functions JavaScript Array Methods
5. Node.js Modules
Modules Introduction CommonJS Modules ES Modules Export and Import Built-in Modules Creating Custom Modules Path Module OS Module
6. Working with JSON
JSON Introduction JSON Syntax JSON.stringify() JSON.parse() JSON Array of Objects JSON User Database
7. Node Package Manager
npm Introduction package.json Installing Packages Dependencies and DevDependencies npm Scripts Package Versions and Semantic Versioning npm init and Project Setup
8. File System
File System Introduction Reading Files Writing Files Appending Files Deleting Files Working with Directories
9. CLI Applications
CLI Applications Introduction Command-Line Input Task Manager Project Structure Reading todos.json Adding, Updating and Deleting Tasks Saving Tasks to JSON
10. Events and EventEmitter
Events Introduction EventEmitter Registering Events Emitting Events Removing Event Listeners Custom Events
11. Asynchronous Programming
Async Programming Introduction Callbacks Promises async and await setTimeout() and setInterval() Event Loop and Non-Blocking I/O
12. Native HTTP Module
HTTP Module Introduction Creating an HTTP Server Handling Requests Sending Responses HTTP Request Methods Status Codes and Headers Handling POST Request Body Parsing Incoming JSON Products JSON API
13. Express.js
Express.js Introduction Installing Express Creating an Express Server Request and Response Objects Route Parameters and Query Strings Serving Static Files
14. Express Middleware
Middleware Introduction Built-in Middleware Application-Level Middleware Router-Level Middleware Custom Middleware Error-Handling Middleware
15. REST API Development
REST API Introduction GET Endpoints POST Endpoints PUT and PATCH Endpoints DELETE Endpoints Request Validation and API Responses
16. Database Integration
Database Introduction MySQL Integration MongoDB Integration Database CRUD Operations Database Queries and Filtering Connection Management
17. Authentication and Authorization
Authentication Introduction Password Hashing User Registration User Login JWT Authentication Authorization and Protected Routes
18. Advanced Node.js
Advanced Node.js Introduction Error Handling Debugging Node.js Applications Streams Buffers Performance and Security

Node.js Fundamentals Introduction

Learn the fundamental concepts of Node.js, understand how JavaScript executes in the Node.js runtime, work with modules, use built-in objects, and prepare to build backend applications.

1. Introduction to Node.js Fundamentals

Node.js fundamentals are the basic concepts required to understand how Node.js applications work. Before building APIs, web servers, or backend applications, we should understand how to execute JavaScript, use modules, access built-in objects, and interact with the runtime environment.

Node.js allows JavaScript to run outside the browser. It provides APIs for tasks such as working with files, accessing environment variables, handling process information, and creating network applications.

Important Concept: Node.js is a JavaScript runtime, not a programming language. We write JavaScript code, and Node.js provides the environment in which that code executes.

Topics Covered in Node.js Fundamentals

Topic Description Example
Running JavaScript Execute JavaScript files using Node.js. node app.js
Global Objects Use objects and functions available in the Node.js environment. console.log()
Process Object Access information about the current Node.js process. process.version
Command-Line Arguments Pass input values when starting an application. process.argv
Environment Variables Read configuration values from the environment. process.env

2. How Node.js Executes JavaScript

Node.js uses the V8 JavaScript engine to execute JavaScript. When we run a JavaScript file, Node.js loads the code and executes it using the runtime environment.

JavaScript Source Code
app.js
↓
Node.js Runtime
↓
V8 JavaScript Engine
↓
JavaScript Executes
↓
Output or Application Behavior

Node.js also provides APIs and runtime facilities that allow JavaScript applications to perform operations such as file access and network communication.

Remember: V8 executes JavaScript. Node.js adds runtime functionality that helps JavaScript applications interact with the operating system and other resources.

3. Write Your First Node.js Program

Let us begin with a simple program that prints messages in the terminal. This example demonstrates how JavaScript executes using Node.js.

Step 1: Create app.js

app.js
console.log("Welcome to Node.js!");
console.log("Learning Node.js Fundamentals.");
console.log("CIIT TRAINING INSTITUTE");

Step 2: Execute the Program

Terminal
node app.js

Expected Output

Welcome to Node.js!
Learning Node.js Fundamentals.
CIIT TRAINING INSTITUTE

Code Explanation

  • console.log() prints information to the console.
  • Text inside quotation marks represents a string.
  • Each statement prints a separate line.
  • node app.js tells Node.js to execute the file.

4. JavaScript and Node.js

JavaScript is the programming language. Node.js is one environment where JavaScript can execute. The same language can also execute in a browser, but the available APIs differ between environments.

Feature Browser JavaScript Node.js JavaScript
Runtime Web browser Node.js runtime
HTML DOM Usually available through browser APIs. Not available by default.
File System Restricted browser APIs. Can use Node.js file-system APIs.
Backend Servers Not the browser's normal role. Can create HTTP servers and backend services.
Environment Variables Not available as Node.js process variables. Can be accessed through process.env.
Practical Example: In a browser, JavaScript can update webpage elements. In Node.js, JavaScript can read files, process data, and build backend APIs using the appropriate Node.js APIs.

5. Important Node.js Capabilities

1. JavaScript Execution

Execute JavaScript files directly from the terminal.

2. File System Access

Read, write, and manage files using the fs module.

3. Module System

Organize code into reusable modules and import functionality when required.

4. Process Information

Inspect the runtime version, arguments, and environment configuration.

5. Network Applications

Build HTTP servers and network-based applications.

6. Package Management

Use npm to install and manage third-party packages.

6. Common Node.js Commands

These commands are useful when working with Node.js projects from the terminal.

Command Purpose
node --version Displays the Node.js version.
npm --version Displays the npm version.
node Opens the interactive REPL when no file is specified.
node app.js Executes the JavaScript file named app.js.
npm init -y Creates a default package.json file.
npm install Installs project dependencies described by the package configuration and lockfile when available.

7. Practical Example: Calculate Student Marks

Let us create a small program that calculates the total and average marks of a student. This example uses basic JavaScript statements executed by Node.js.

Step 1: Create student.js

student.js
const studentName = "Student";

const subject1 = 80;
const subject2 = 75;
const subject3 = 90;

const total = subject1 + subject2 + subject3;
const average = total / 3;

console.log("Student:", studentName);
console.log("Total Marks:", total);
console.log("Average Marks:", average);

Step 2: Run the Program

Terminal
node student.js

Expected Output

Student: Student
Total Marks: 245
Average Marks: 81.66666666666667

Explanation

  • const declares variables whose bindings cannot be reassigned.
  • The total is calculated by adding the three subject marks.
  • The average is calculated by dividing the total by three.
  • console.log() displays the calculated results.
Learning Point: This example demonstrates how Node.js can execute ordinary JavaScript calculations without a browser.

8. Best Practices for Beginners

  • Use meaningful variable and function names.
  • Keep application code organized into separate files and modules.
  • Read terminal error messages carefully.
  • Use npm to manage project dependencies.
  • Keep configuration values separate from application logic.
  • Avoid placing passwords, tokens, or other secrets directly in source code.
  • Practice each concept with small JavaScript programs before building larger applications.
Important: Do not assume every browser API exists in Node.js. Always check which APIs are available in the runtime you are using.

9. CIIT Training Institute - Practice Task

Task: Create a Course Information Program

Create a file named course.js and complete the following activities:

  1. Create a variable to store the institute name.
  2. Create a variable to store a course name.
  3. Store the course duration in months.
  4. Calculate the total fee for two course installments.
  5. Print all the information using console.log().
  6. Execute the file using node course.js.
Sample Program
course.js
const institute = "CIIT TRAINING INSTITUTE";
const course = "Node.js";
const duration = 3;

const installment1 = 10000;
const installment2 = 10000;
const totalFee = installment1 + installment2;

console.log("Institute:", institute);
console.log("Course:", course);
console.log("Duration:", duration, "months");
console.log("Total Fee:", totalFee);
Expected Output
Institute: CIIT TRAINING INSTITUTE
Course: Node.js
Duration: 3 months
Total Fee: 20000

Summary

Node.js fundamentals explain how JavaScript runs outside the browser and how the Node.js runtime provides APIs for application development. We learned about JavaScript execution, the runtime and V8 engine, common terminal commands, built-in capabilities, and practical calculations. Understanding these concepts prepares students at CIIT TRAINING INSTITUTE to continue learning JavaScript execution, global objects, process information, command-line arguments, and environment variables.