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

Installing Express.js

Express.js is a web framework for Node.js that makes it easier to create web applications and APIs. Before using Express.js in a project, we need to create a Node.js project and install the Express package.

In this lesson, we will learn how to check Node.js and npm, create a project folder, initialize the project, install Express.js, verify the installation, and run a basic Express application.


1. What Is Express.js Installation?

Installing Express.js means adding the Express package to a Node.js project so that we can use its routing, request-handling, and response-handling features.

For example, Samadhan wants to create a course information API for CIIT Training Institute. First, he installs Node.js, creates a project, and then installs Express.js to build the API.

Terminal
npm install express

This command installs Express.js and records it as a dependency in the project's package.json file.

2. Prerequisites for Installing Express.js

Before installing Express.js, make sure Node.js and npm are available on your computer.

Requirement Purpose Verification Command
Node.js Runs JavaScript outside the browser. node -v
npm Installs and manages project packages. npm -v
Code Editor Used to create and edit project files. Visual Studio Code or another editor

Step 1: Check the Node.js version.

Terminal
node -v

Example output:

Terminal Output
v22.0.0

The displayed version depends on the version installed on your computer. The output above is only an example.

Step 2: Check the npm version.

Terminal
npm -v

Example output:

Terminal Output
10.8.0

If these commands display version numbers, Node.js and npm are available in your terminal. If a command is not recognized, check the installation and PATH configuration.

3. Creating a Node.js Project

Before installing Express.js, create a separate folder for the project. This keeps the source code and dependencies organized.

Step 1: Create a project folder.

Terminal
mkdir express-demo

The mkdir command creates a folder named express-demo.

Step 2: Open the project folder.

Terminal
cd express-demo

The cd command changes the current working directory to the project folder.

4. Project Setup Flow

The following diagram shows the steps involved in preparing a project before installing Express.js.

Check Node.js and npm

Verify the development environment

↓
Create Project Folder

mkdir express-demo

↓
Open Project Folder

cd express-demo

↓
Initialize the Project

npm init -y

5. Initializing the Project with npm

The npm init command initializes a Node.js project by creating a package.json file. This file stores project metadata, scripts, and dependency information.

Command:

Terminal
npm init -y

The -y option accepts the default answers to the initialization questions.

Example package.json:

package.json
{
  "name": "express-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The generated values may differ depending on the folder name and npm version. The important point is that the project now has a package.json file.

6. Installing Express.js

Once the project is initialized, install Express.js using npm.

Terminal
npm install express

npm downloads Express.js and the required dependencies, places them in the node_modules folder, and updates the project dependency information.

After installation, the project structure looks like this:

Project Structure
express-demo/
|
|-- node_modules/
|
|-- package.json
|
|-- package-lock.json
|
File or Folder Purpose
node_modules Contains installed packages and their dependencies.
package.json Contains project information and declared dependencies.
package-lock.json Records the resolved dependency versions for reproducible installs.

7. Verifying the Express.js Installation

After installation, we can check whether Express.js appears in the project's dependency tree.

Terminal
npm list express

Example output:

Terminal Output
express-demo@1.0.0
`-- express@4.x.x

The version shown in your terminal may be different. A successful installation means the package is available to the project.

You can also inspect package.json and look for Express.js under dependencies.

8. Creating Your First Express Server

Now that Express.js is installed, create a file named server.js inside the project folder.

server.js
const express = require('express');

const app = express();

app.get('/', (req, res) => {
    res.send('Express.js installed successfully!');
});

const PORT = 3000;

app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}`);
});

Explanation:

Code Explanation
require('express') Imports the installed Express.js package.
express() Creates an Express application.
app.get('/', ...) Defines a GET route for the home page.
res.send() Sends a response to the client.
app.listen() Starts the server on the specified port.

9. How the Installed Express Server Works

When the application starts, it listens for incoming requests. If the client opens the home page, Express.js matches the route and returns the configured message.

Start Server

node server.js

↓
Express Listens on Port 3000

Waits for incoming HTTP requests

↓
Browser Requests the Home Page

GET /

↓
Express Route Executes

res.send()

↓
Browser Displays the Response

Express.js installed successfully!

10. Running and Testing the Application

Open a terminal in the folder containing server.js and execute the following command.

Terminal
node server.js

Expected terminal output:

Terminal Output
Server running at http://localhost:3000

Open the following URL in your browser:

Browser URL
http://localhost:3000/

Expected browser output:

Browser Output
Express.js installed successfully!

If the message appears in the browser, Express.js is installed and your first Express server is working.

11. Common Installation Problems

Problem Possible Reason What to Check
node is not recognized Node.js is not installed or PATH is not configured. Install Node.js and reopen the terminal.
npm is not recognized npm is unavailable in the terminal. Check the Node.js installation and PATH.
Cannot find module 'express' Express is not available to the running project. Run npm install express in the project folder.
EADDRINUSE The port is already in use. Stop the other server or use another available port.

12. Practice Tasks

Task 1: Install Express.js

  1. Check Node.js and npm versions.
  2. Create a folder named my-express-app.
  3. Initialize the project using npm init -y.
  4. Install Express.js using npm install express.
  5. Verify the installation using npm list express.

Expected result: Express.js should appear in the project's installed dependencies.

Task 2: Run an Express Server

  1. Create a file named server.js.
  2. Import Express.js and create an application.
  3. Define a GET route for /.
  4. Return a welcome message using res.send().
  5. Start the server and test it in the browser.

Expected result: The browser should display the welcome message returned by the Express application.

Summary

In this lesson, we learned how to prepare a Node.js project, initialize it using npm, install Express.js, and verify the installation. We also created and tested a basic Express server using a GET route.

Remember the flow: Check Node.js and npm → Create Project → Initialize with npm → Install Express → Create Server → Test in Browser.