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

JavaScript Basics Introduction

Learn the fundamentals of JavaScript, including variables, data types, operators, conditions, loops, and functions. JavaScript is an essential programming language for web development and Node.js applications.

1. What Is JavaScript?

JavaScript is a programming language used to create interactive and dynamic applications. It can run in web browsers and on servers through environments such as Node.js.

JavaScript allows developers to perform calculations, validate forms, handle events, manipulate web pages, communicate with APIs, and build server-side applications.

Common uses of JavaScript:

  • Creating interactive web pages.
  • Developing server-side applications using Node.js.
  • Building web APIs and backend services.
  • Validating user input and processing data.
  • Developing frontend applications using JavaScript frameworks.
Remember: JavaScript and Java are different programming languages. Their names are similar, but their syntax, ecosystem, and uses differ.

2. How JavaScript Works

A JavaScript program contains instructions. A compatible JavaScript runtime executes those instructions and produces results.

JavaScript Code

Variables, expressions, functions, and statements

↓
JavaScript Runtime

Browser or Node.js

↓
Program Execution

Output, calculations, or application behavior

Note: This is a simplified learning diagram. The actual execution process depends on the JavaScript runtime and the program.

3. Your First JavaScript Program

Let us start with a simple program that displays a message in the terminal using console.log().

Step 1: Create a File

Create a file named hello.js.

Step 2: Write the Code

JavaScript
console.log("Hello, JavaScript!");
console.log("Welcome to CIIT Training Institute.");

Step 3: Run the Program

Terminal
node hello.js

Step 4: Expected Output

Output
Hello, JavaScript!
Welcome to CIIT Training Institute.

Explanation: The console.log() function displays the supplied values in the terminal. Each call prints its message on a new line.

4. Variables in JavaScript

Variables are used to store values that a program can access and process. JavaScript provides three commonly used declarations: var, let, and const.

Keyword Description Reassignment
var Function-scoped or globally scoped declaration. Allowed.
let Block-scoped variable declaration. Allowed.
const Block-scoped binding that must be initialized. Not allowed for the binding.

Example

JavaScript
let studentName = "Samadhan";
let age = 22;

const instituteName = "CIIT Training Institute";

console.log(studentName);
console.log(age);
console.log(instituteName);

Output

Output
Samadhan
22
CIIT Training Institute

Explanation: The variables store a student name, age, and institute name. We use console.log() to display their values.

5. JavaScript Data Types

A data type describes the kind of value stored or processed by a program. JavaScript includes primitive types and objects.

Data Type Example Description
String "Hello" Text values.
Number 100 Numeric values, including integers and decimals.
Boolean true Logical true or false values.
Undefined undefined A value indicating that a variable has not been assigned a value.
Null null An intentional absence of a value.
BigInt 123n Integers larger than the safe integer range of Number can represent precisely.
Symbol Symbol("id") A unique symbol value.
Object { "{ name: 'Samadhan' }" } Stores related properties and values. Arrays and functions are also objects.

Example

JavaScript
let name = "Samadhan";
let age = 22;
let isStudent = true;
let result;

console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);
console.log(typeof result);

Output

Output
string
number
boolean
undefined

6. JavaScript Operators

Operators perform operations on values and variables. They are used in calculations, comparisons, and logical expressions.

Operator Type Examples Purpose
Arithmetic + - * / % Performs mathematical operations.
Comparison === !== > < Compares values.
Logical && || ! Combines or reverses logical conditions.
Assignment = += -= Assigns or updates values.

Example

JavaScript
let a = 10;
let b = 3;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a % b);
console.log(a > b);
console.log(a === b);

Output

Output
13
7
30
1
true
false

7. Conditions in JavaScript

Conditional statements execute different blocks of code depending on whether a condition is true or false.

Example: if-else

JavaScript
let marks = 75;

if (marks >= 40) {
    console.log("Student passed");
} else {
    console.log("Student failed");
}

Output

Output
Student passed

Explanation: Since marks is 75, the condition marks >= 40 is true. Therefore, the first block executes.

8. Loops in JavaScript

Loops execute a block of code repeatedly while a condition is satisfied or while items remain to be processed.

Example: for Loop

JavaScript
for (let i = 1; i <= 5; i++) {
    console.log(i);
}

Output

Output
1
2
3
4
5

The loop starts with i = 1, continues while i <= 5, and increases i by one after each iteration.

9. Functions in JavaScript

A function is a reusable block of code that performs a particular task. Functions can accept parameters and return values.

Example

JavaScript
function add(a, b) {
    return a + b;
}

let result = add(10, 20);

console.log("Sum:", result);

Output

Output
Sum: 30

Explanation: The function receives two parameters, adds their values, and returns the result. The returned value is then displayed.

10. Practice Task

Student Result Program

Create a JavaScript program that:

  • Stores a student's name and marks in variables.
  • Displays the student's name and marks.
  • Uses an if-else statement to check whether the student passed.
  • Uses a loop to display numbers from 1 to 5.
  • Creates a function that calculates the total of two numbers.
Challenge: Extend the program to calculate the average of three subjects and display the result using a function.

Important Points

  • JavaScript is used in browsers and Node.js applications.
  • Use let for reassigned variables and const for bindings that should not be reassigned.
  • JavaScript supports primitive data types and objects.
  • Operators perform calculations and comparisons.
  • Conditional statements control program flow.
  • Loops repeat instructions.
  • Functions make code reusable and organized.

Summary

JavaScript is a programming language used to build interactive web applications and server-side programs with Node.js. In this introduction, we learned about variables, data types, operators, conditions, loops, and functions. These fundamentals help us understand more advanced JavaScript concepts and develop practical applications.