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 Performance Optimization and Security

Performance optimization helps Node.js applications respond faster and use resources efficiently. Security protects applications, APIs, and user data from unauthorized access and common attacks. In this tutorial, we will explore performance measurement, asynchronous programming, caching, compression, input validation, rate limiting, and security best practices.

1. What Is Performance Optimization?

Performance optimization is the process of improving an application's response time, throughput, resource usage, and ability to handle concurrent requests.

For example, if an API takes several seconds to respond because it performs unnecessary database queries, optimization can reduce the number of queries, improve indexing, or cache frequently requested data.

Performance Factor Meaning Improvement Technique
Response Time Time taken to return a response. Optimize queries and avoid unnecessary work.
Throughput Requests processed over a period. Reduce bottlenecks and improve resource utilization.
Memory Usage Memory consumed by the application. Use streams and avoid retaining unnecessary objects.
CPU Usage Processor time used by application tasks. Profile expensive computations and optimize algorithms.
Scalability Ability to handle increased demand. Use appropriate caching, databases, and deployment strategies.

2. Why Is Performance Optimization Important?

  • Improves API response times.
  • Helps applications handle more concurrent users.
  • Reduces unnecessary CPU and memory consumption.
  • Improves user experience.
  • Helps reduce infrastructure costs.
  • Improves application reliability under load.
Remember: Measure performance before optimizing. Profiling and realistic load testing help identify the actual bottleneck instead of making unnecessary changes.

3. Avoid Blocking the Event Loop

Node.js uses an event loop to coordinate JavaScript execution and asynchronous operations. Long-running synchronous work can block the event loop and delay other requests.

Consider a CPU-intensive loop. While JavaScript is busy executing it, the same event-loop thread cannot process other JavaScript callbacks.

JavaScript — blocking-example.js
const start = Date.now();

while (Date.now() - start < 3000) {
    // Simulates blocking work for about 3 seconds.
}

console.log("Blocking operation completed");

Run the Program

Terminal
node blocking-example.js

Output

Terminal
Blocking operation completed

The program occupies the JavaScript thread for approximately three seconds. During that time, other JavaScript callbacks in the same event loop are delayed.

Better Approach

  • Use asynchronous APIs instead of synchronous file or network operations where appropriate.
  • Use streams to process large files incrementally.
  • Move expensive CPU calculations to worker threads or separate services when appropriate.
  • Break large computations into manageable tasks when the workload allows it.
Important: Using async does not automatically make CPU-intensive JavaScript non-blocking. A long synchronous calculation inside an async function can still block the event loop.

4. Use Asynchronous Operations

Asynchronous APIs allow Node.js to coordinate I/O without unnecessarily blocking JavaScript while waiting for a result.

JavaScript — async-file.js
const fs = require("fs");

console.log("Program started");

fs.readFile("input.txt", "utf8", (error, data) => {
    if (error) {
        console.error("File read failed:", error.message);
        return;
    }

    console.log("File content:", data);
});

console.log("Read request submitted");

Expected Output

Assuming input.txt exists, a typical output order is:

Terminal
Program started
Read request submitted
File content: [contents of input.txt]

The file-reading callback runs after the asynchronous read completes. The actual completion time depends on the file and system.

5. Use Streams for Large Files

Reading a large file entirely into memory can consume substantial resources. Streams allow an application to process file data incrementally.

JavaScript — stream-file.js
const fs = require("fs");
const { pipeline } = require("stream");

pipeline(
    fs.createReadStream("large.txt"),
    fs.createWriteStream("large-copy.txt"),
    (error) => {
        if (error) {
            console.error("Copy failed:", error.message);
            return;
        }

        console.log("Large file copied successfully");
    }
);

Run the Program

Terminal
node stream-file.js

Expected Output

Terminal
Large file copied successfully

This example assumes large.txt exists and the application has permission to read and write the files. Streams are useful when the data is too large to process comfortably in memory all at once.

6. Caching in Node.js

Caching stores the result of an operation so repeated requests can reuse it instead of performing the same expensive work every time.

An application might cache configuration data, frequently requested public information, or results from expensive calculations.

JavaScript — cache-example.js
const cache = new Map();

function getProduct(id) {
    if (cache.has(id)) {
        console.log("Returning cached product");
        return cache.get(id);
    }

    console.log("Loading product");

    const product = {
        id,
        name: "Laptop",
        price: 50000
    };

    cache.set(id, product);

    return product;
}

console.log(getProduct(1));
console.log(getProduct(1));

Output

Terminal
Loading product
{ id: 1, name: 'Laptop', price: 50000 }
Returning cached product
{ id: 1, name: 'Laptop', price: 50000 }

The second call reuses the cached object rather than creating it again. This example uses in-memory caching for learning purposes.

Production note: In-memory caches are limited to a process and can grow without bound if not managed. Consider expiration, size limits, invalidation, and a shared cache such as Redis when multiple application instances need to share cached data.

7. Response Compression in Express.js

Compression can reduce the size of supported HTTP responses, helping decrease network transfer time. It is most useful for compressible text-based responses such as HTML, CSS, JavaScript, and JSON.

Install the Package

Terminal
npm install express compression

Example

JavaScript — app.js
const express = require("express");
const compression = require("compression");

const app = express();

app.use(compression());

app.get("/api/message", (req, res) => {
    res.json({
        message: "Response compression is enabled.",
        status: "success"
    });
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});

Test the API

URL
http://localhost:3000/api/message

Expected JSON Response

JSON
{
    "message": "Response compression is enabled.",
    "status": "success"
}

The compression middleware negotiates supported encodings with the client. Whether a particular response is compressed depends on response size, content type, request headers, and middleware settings.

8. Improve Database Query Performance

Database operations can become a bottleneck when queries retrieve too much data, scan large tables, or run repeatedly without a need.

  • Retrieve only the columns the application needs.
  • Use indexes for appropriate filtering and sorting patterns.
  • Use pagination when returning large result sets.
  • Avoid unnecessary repeated queries.
  • Use connection pooling where supported.
  • Inspect slow queries and query execution plans.
SQL Example
SELECT id, name, email
FROM users
WHERE email = ?;

A parameterized query helps separate SQL structure from user-supplied values. An appropriate index on the filtered column may improve performance, depending on the schema, query plan, and database engine.

9. What Is Security in Node.js?

Security means protecting an application, its data, and its users from unauthorized access, misuse, and malicious input.

Common security risks include:

  • SQL injection.
  • Cross-site scripting (XSS).
  • Broken authentication and authorization.
  • Excessive requests and denial-of-service risks.
  • Exposed secrets and credentials.
  • Unsafe dependencies and outdated packages.
  • Insufficient input validation.

10. Validate User Input

Always validate request data before using it. Never assume that values received from a client are valid or trustworthy.

JavaScript — validation.js
const express = require("express");

const app = express();

app.use(express.json());

app.post("/api/users", (req, res) => {
    const { name, email } = req.body;

    if (
        typeof name !== "string" ||
        typeof email !== "string" ||
        !name.trim() ||
        !email.trim()
    ) {
        return res.status(400).json({
            message: "Valid name and email are required."
        });
    }

    return res.status(201).json({
        message: "Input accepted.",
        user: {
            name: name.trim(),
            email: email.trim()
        }
    });
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});

Test with Postman

Send a POST request to:

URL
http://localhost:3000/api/users

Use Body → raw → JSON:

JSON
{
    "name": "Amit",
    "email": "amit@example.com"
}

Expected Response

JSON
{
    "message": "Input accepted.",
    "user": {
        "name": "Amit",
        "email": "amit@example.com"
    }
}

This is a basic demonstration. A production application should also validate email format, enforce length limits, handle malformed JSON, and apply the business rules required by the application.

11. Prevent SQL Injection

SQL injection occurs when untrusted input is incorporated into SQL in a way that changes the intended query. Parameterized queries help prevent this.

Unsafe Pattern

JavaScript — Avoid This
const query =
    "SELECT * FROM users WHERE email = '" + email + "'";

This approach mixes user input with SQL text and can create an injection vulnerability.

Safer Pattern with mysql2

JavaScript
const [users] = await db.execute(
    "SELECT id, name, email FROM users WHERE email = ?",
    [email]
);

The placeholder keeps the query structure separate from the supplied email. Always use parameterized queries for user-provided values.

12. Rate Limiting in Express.js

Rate limiting restricts how many requests a client can make during a given period. It can reduce abusive traffic and protect sensitive endpoints such as login and registration routes.

Install the Package

Terminal
npm install express-rate-limit

Example

JavaScript — rate-limit.js
const express = require("express");
const rateLimit = require("express-rate-limit");

const app = express();

app.use(express.json());

const loginLimiter = rateLimit({
    windowMs: 15 * 60 * 1000,
    limit: 10,
    standardHeaders: "draft-8",
    legacyHeaders: false,
    message: {
        message: "Too many login attempts. Please try again later."
    }
});

app.post("/api/login", loginLimiter, (req, res) => {
    res.json({
        message: "Login endpoint reached."
    });
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});

This example configures a limit of ten requests per window for each client as identified by the limiter's configured key generator.

Production note: Configure proxy trust and client IP handling carefully. For multi-instance deployments, consider an appropriate shared rate-limit store so limits are not isolated to each process.

13. Secure HTTP Headers with Helmet

Helmet is Express middleware that sets various HTTP security headers. These headers help reduce exposure to certain web vulnerabilities when configured appropriately for the application.

Install Helmet

Terminal
npm install helmet

Example

JavaScript — helmet-example.js
const express = require("express");
const helmet = require("helmet");

const app = express();

app.use(helmet());

app.get("/", (req, res) => {
    res.send("Security headers are configured.");
});

app.listen(3000, () => {
    console.log("Server running on port 3000");
});

Helmet configures several headers by default. Review its configuration against your application's requirements, particularly if the application serves browser content that depends on specific content security policies.

14. Protect Secrets Using Environment Variables

Database passwords, API keys, and JWT signing secrets should not be hard-coded into application source files.

Create a local .env file for development:

.env
PORT=3000
DB_HOST=localhost
DB_USER=app_user
DB_PASSWORD=replace_with_a_secret
JWT_SECRET=replace_with_a_strong_random_secret

Load the environment variables before using them:

JavaScript
require("dotenv").config();

const port = process.env.PORT || 3000;

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});
  • Add .env to .gitignore.
  • Never commit real secrets to source control.
  • Use a secrets manager or deployment environment configuration in production.
  • Rotate exposed credentials immediately.

15. Authentication and Authorization

A secure application must verify who the user is and what the user is allowed to do. Authentication alone does not grant permission to every resource.

Concept Purpose Example
Authentication Verifies user identity. Checking a JWT or login credentials.
Authorization Checks permissions. Allowing only Admin users to delete records.
  • Verify tokens and session credentials correctly.
  • Check authorization on every protected endpoint.
  • Do not trust roles supplied directly by the client.
  • Restrict users to their own data unless broader access is authorized.
  • Use the principle of least privilege.

16. Performance Optimization Workflow

Step 1: Measure

Collect response-time, CPU, and memory information.

↓
Step 2: Find the Bottleneck

Inspect slow queries, expensive functions, or network delays.

↓
Step 3: Optimize

Improve algorithms, caching, database access, or data flow.

↓
Step 4: Test Again

Compare results under realistic workloads.

17. Node.js Security Checklist

  • Validate and normalize incoming input.
  • Use parameterized SQL queries.
  • Use HTTPS in production.
  • Store passwords using a suitable password-hashing algorithm.
  • Use authentication and authorization on protected routes.
  • Apply rate limits to sensitive endpoints.
  • Use security headers where appropriate.
  • Keep dependencies updated and review vulnerabilities.
  • Keep secrets out of source control and logs.
  • Handle errors without exposing stack traces to clients.
  • Set request-size limits appropriate to the application.
  • Use logging and monitoring to detect suspicious behavior.

Important Points to Remember

  • Measure performance before making optimizations.
  • Avoid long synchronous operations that block the event loop.
  • Use streams for large data-processing tasks.
  • Caching can reduce repeated work when managed correctly.
  • Compression can reduce network transfer sizes.
  • Validate incoming data and use parameterized database queries.
  • Use rate limiting and appropriate security headers.
  • Protect credentials with environment configuration or a secrets manager.
  • Authentication and authorization solve different problems.

Summary

Performance optimization helps Node.js applications use resources efficiently and respond reliably under load. Security practices such as input validation, parameterized queries, rate limiting, secure headers, and secret management help protect application data and APIs. Measuring, testing, and monitoring are essential for maintaining a secure, production-ready application.

← Previous: Buffers