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

Buffers in Node.js

Buffers are used to work with binary data in Node.js. They allow applications to store and manipulate raw bytes, which are useful when reading files, processing images, handling network data, and working with streams. In this tutorial, we will explore Buffer operations with practical examples.

1. What Is a Buffer in Node.js?

A Buffer is a Node.js object that represents a sequence of bytes. A byte is a unit of data consisting of 8 bits.

JavaScript strings represent text, but many operations involve binary data, such as images, audio, video, compressed files, and network packets. Buffers allow Node.js to handle this data directly.

Simple example: A text string such as "Hello" can be encoded into bytes using a Buffer. Those bytes can then be written to a file or sent over a network connection.

2. Why Do We Use Buffers?

  • To read and write binary file contents.
  • To process data received from network connections.
  • To handle binary chunks from streams.
  • To encode and decode text as bytes.
  • To work with images, audio, video, and other binary formats.
  • To convert between supported encodings such as UTF-8 and hexadecimal.
Data Type Purpose Example
String Represents text. "Hello Node.js"
Buffer Represents a sequence of bytes. Bytes that encode the text or binary file data.
Stream Processes data over time, often in chunks. Reading a large file incrementally.

3. Creating a Buffer Using Buffer.from()

The Buffer.from() method creates a Buffer from existing data, such as a string or an array of byte values.

JavaScript — buffer-from.js
const buffer = Buffer.from("Hello Node.js");

console.log(buffer);
console.log(buffer.toString());

Run the Program

Terminal
node buffer-from.js

Output

Terminal
<Buffer 48 65 6c 6c 6f 20 4e 6f 64 65 2e 6a 73>
Hello Node.js

The hexadecimal values represent the encoded bytes of the text. For example, the uppercase letter H has the UTF-8 byte value 48 in hexadecimal.

4. Creating a Buffer from an Array

You can create a Buffer from an array of numeric byte values. Each value must be in the byte range from 0 to 255.

JavaScript
const buffer = Buffer.from([72, 101, 108, 108, 111]);

console.log(buffer);
console.log(buffer.toString("utf8"));

Output

Terminal
<Buffer 48 65 6c 6c 6f>
Hello

The numeric values correspond to the UTF-8 bytes of the word Hello.

5. Creating a Buffer Using Buffer.alloc()

The Buffer.alloc() method creates a Buffer of a specified size and initializes its bytes to zero by default.

JavaScript
const buffer = Buffer.alloc(5);

console.log(buffer);

buffer.write("Hello");

console.log(buffer.toString());

Output

Terminal
<Buffer 00 00 00 00 00>
Hello

Initially, the Buffer contains five zero bytes. The write() method writes the five ASCII-compatible bytes of Hello into it.

Security note: Prefer Buffer.alloc() when you need a newly initialized Buffer. Buffer.allocUnsafe() may contain old memory data until it is overwritten, so never expose its contents before initializing them.

6. Understanding Buffer Length

The length property returns the number of bytes in a Buffer. It does not necessarily return the number of characters in the original text.

JavaScript
const buffer = Buffer.from("Hello");

console.log("Buffer length:", buffer.length);

Output

Terminal
Buffer length: 5

The word Hello contains five ASCII characters, and each character uses one byte in UTF-8, so the Buffer length is five.

7. Difference Between String Length and Buffer Length

In UTF-8, some characters require more than one byte. This is why the number of characters and the number of bytes can be different.

JavaScript
const text = "A€";

const buffer = Buffer.from(text, "utf8");

console.log("String length:", text.length);
console.log("Buffer length:", buffer.length);
console.log("Bytes:", buffer);

Output

Terminal
String length: 2
Buffer length: 4
Bytes: <Buffer 41 e2 82 ac>

The string contains two UTF-16 code units: one for A and one for €. In UTF-8, A uses one byte and € uses three bytes, giving a total Buffer length of four.

8. Converting a Buffer to a String

The toString() method decodes Buffer bytes using a specified encoding. UTF-8 is the default encoding.

JavaScript
const buffer = Buffer.from("Hello Node.js");

console.log(buffer.toString("utf8"));
console.log(buffer.toString("hex"));
console.log(buffer.toString("base64"));

Output

Terminal
Hello Node.js
48656c6c6f204e6f64652e6a73
SGVsbG8gTm9kZS5qcw==
Encoding Purpose
utf8 Decodes bytes as UTF-8 text.
hex Represents each byte using hexadecimal digits.
base64 Represents binary data using Base64 text.
ascii Decodes bytes using ASCII-compatible rules.

Base64 is an encoding format, not encryption. Do not use it to protect passwords or other sensitive information.

9. Accessing and Modifying Buffer Bytes

Buffer bytes can be accessed and modified using numeric indexes. Indexing starts at zero.

JavaScript
const buffer = Buffer.from("Hello");

console.log(buffer[0]);
console.log(buffer[1]);

buffer[0] = 74;

console.log(buffer.toString());

Output

Terminal
72
101
Jello

The first byte of Hello is 72, which represents H. Assigning 74 changes that byte to J.

10. Copying Buffer Data

The copy() method copies bytes from one Buffer into another.

JavaScript
const source = Buffer.from("Hello");
const target = Buffer.alloc(5);

source.copy(target);

console.log(target.toString());

Output

Terminal
Hello

The five bytes from source are copied into target. The destination Buffer must have enough space for the bytes you intend to copy.

11. Using subarray() with Buffers

The subarray() method creates a view of a portion of the original Buffer. The returned view shares the same underlying memory.

JavaScript
const buffer = Buffer.from("Hello");
const part = buffer.subarray(0, 2);

console.log(part.toString());

part[0] = 74;

console.log(buffer.toString());

Output

Terminal
He
Jello

Changing the first byte of part also changes the original Buffer because the view shares its memory. If you need independent bytes, create a copy instead.

12. Reading a File into a Buffer

Node.js can read an entire file into memory using fs.readFile(). When no text encoding is specified, the callback receives a Buffer.

JavaScript — read-buffer.js
const fs = require("fs");

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

    console.log("Buffer:", data);
    console.log("Content:", data.toString("utf8"));
    console.log("Size:", data.length, "bytes");
});

This example assumes input.txt exists in the current working directory. The Buffer output depends on the file's actual contents.

Important: fs.readFile() loads the entire file into memory. For very large files, use a Readable stream instead.

13. Difference Between Buffer and Stream

Buffer Stream
Represents a sequence of bytes. Provides an interface for processing data over time.
Can hold a complete piece of binary data. Can process data in chunks.
Often used to store or manipulate byte data. Often used to read, write, or transfer data incrementally.
Example: Buffer.from("Hello") Example: fs.createReadStream("input.txt")

Streams often deliver data as Buffers by default, although they can also operate in text mode or use other object representations.

14. How Buffer Processing Works

Data Source

A file, network connection, or string.

↓
Buffer

Stores the data as bytes.

↓
Processing

Read, modify, copy, or encode the bytes.

↓
Output

Convert to text, write a file, or send the bytes onward.

15. Buffer Best Practices

  • Use Buffer.from() when creating a Buffer from known data.
  • Use Buffer.alloc() when you need an initialized Buffer of a given size.
  • Avoid exposing data from an uninitialized Buffer.
  • Remember that Buffer length is measured in bytes.
  • Choose the correct encoding when converting between text and bytes.
  • Be aware that subarray() shares memory with the original Buffer.
  • Use streams for large files when loading everything into memory is undesirable.
  • Validate incoming binary data before trusting or processing it.

Important Points to Remember

  • A Buffer represents a sequence of bytes.
  • Buffer.from() creates a Buffer from existing data.
  • Buffer.alloc() creates an initialized Buffer.
  • buffer.length returns the number of bytes.
  • toString() converts bytes into a string using an encoding.
  • copy() copies bytes to another Buffer.
  • subarray() creates a view that shares the underlying memory.
  • Buffers and streams are related but serve different purposes.

Summary

Buffers allow Node.js applications to work directly with binary data. Methods such as from(), alloc(), toString(), copy(), and subarray() make it possible to create, decode, copy, and inspect bytes. Understanding Buffers is useful when working with files, streams, network data, and binary formats.