KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
Node.jsNode.js Tutorial
Lesson

Node.js Buffer

10 min reading
Free Course

Node.js Buffer: Working with Raw Binary Data

JavaScript strings use UTF-16 text encoding, which cannot cleanly represent raw TCP packets, PNG images, or cryptographic hashes. Node.js provides the global Buffer class—a fixed-length chunk of memory backed by Uint8Array—to read, convert, and manipulate raw binary bytes directly.

Creating and Converting Buffers

Always use the safe factory methods Buffer.from(), Buffer.alloc(), and Buffer.concat(). Never use the deprecated new Buffer() constructor, which had security leaks on uninitialized memory.

// 1. Convert UTF-8 text to a Buffer and encode as Base64 or Hex
const textBuf = Buffer.from('Node.js Binary Data', 'utf8');
console.log('Byte length:', textBuf.length);
console.log('Base64:', textBuf.toString('base64'));
console.log('Hex:', textBuf.toString('hex'));

// 2. Decode Base64 back into UTF-8 text
const decoded = Buffer.from(textBuf.toString('base64'), 'base64').toString('utf8');
console.log('Decoded:', decoded);

// 3. Allocate a zero-filled 8-byte buffer
const emptyBuf = Buffer.alloc(8);
emptyBuf.writeUInt16BE(404, 0);
console.log('First 2 bytes:', emptyBuf.readUInt16BE(0));

Checking Byte Length vs String Character Length

Because multi-byte UTF-8 characters like emojis or Bangla letters use 2 to 4 bytes each, 'Hi 🚀'.length is 5 UTF-16 code units, whereas Buffer.byteLength('Hi 🚀', 'utf8') is 7 actual bytes. Always use Buffer.byteLength() when setting HTTP Content-Length headers or checking payload limits.

Common Mistakes to Avoid

  • Using Buffer.allocUnsafe() without overwriting every byte: allocUnsafe() skips zeroing RAM for speed and may contain leftover data from earlier memory allocations. Stick to Buffer.alloc() unless writing performance-critical low-level parsers.
  • Splitting multi-byte UTF-8 chunks across stream packets naively: Calling chunk.toString('utf8') on arbitrary TCP chunks can split a 4-byte emoji in half and produce `` replacement characters. Use StringDecoder or collect chunks with Buffer.concat() first.
  • Comparing secret tokens with === on strings: Use crypto.timingSafeEqual(bufA, bufB) on equal-length Buffers to prevent timing attacks.

Frequently Asked Questions

Is Buffer a subclass of Uint8Array?

Yes. Every Node.js Buffer is an instance of Uint8Array, so standard typed-array methods and Web APIs like TextEncoder interoperate cleanly.

How do I combine an array of Buffer chunks from a stream?

Call Buffer.concat(chunks) once the stream emits 'end', which allocates one combined Buffer efficiently.

Do I need to import Buffer in Node.js?

Buffer is available globally, though importing import { Buffer } from 'node:buffer' makes dependencies explicit in TypeScript and ESM codebases.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js Email
Continue JourneyTake Recap Quiz

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum