Chapter 3

Working with Buffers

IN THIS CHAPTER

Bullet Taking in some buffer basics

Bullet Creating and decoding buffers

“At its most fundamental, information is a binary choice. In other words, a single bit of information is one yes-or-no choice.”

—JAMES GLEICK

The Buffer object in Node.js is a global that provides a way to work with streams of raw data stored outside of the V8 engine. Buffers are necessary in Node.js because server-side programs regularly read data from the file system, from databases, and from network connections.

Knowing Your Buffer Basics

To understand the purpose of Buffer, try changing the file extension of a .png image to .txt and opening it in VS Code (or any other text editor). What you see is a garbled mess of seemingly random characters along with some textual metadata, as shown in Figure 3-1.

If you save that text file with the .png extension, you'll most likely no longer have an image file that can be displayed in a photo editor. The process of converting the image (binary data) to text messes up the precise sequence of data that makes an image file an image.

Screenshot of reading binary data as text.

FIGURE 3-1: Reading binary data as text.

Buffer objects hold a representation of raw binary data so that ...

Get JavaScript All-in-One For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.