Do you want a better understanding of Buffer in Node.js? Check this out.

Justice Mba
Dec 13, 2017 · 9 min read

… mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.

Binary data, what’s that?

Character Sets

Character Encoding

Stream

Buffer

Interacting with a Buffer

// Create an empty buffer of size 10. 
// A buffer that only can accommodate 10 bytes.
const buf1 = Buffer.alloc(10);// Create a buffer with contentconst buf2 = Buffer.from("hello buffer");
// Examine the structure of a bufferbuf1.toJSON()
// { type: 'Buffer', data: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }
// an empty buffer
buf2.toJSON()
// { type: 'Buffer',
data: [
104, 101, 108, 108, 111, 32, 98, 117, 102, 102, 101, 114
]
}
// the toJSON() method presents the data as the Unicode Code Points of the characters// Examine the size of a bufferbuf1.length // 10buf2.length // 12. Auto-assigned based on the initial content when created.// Write to a buffer
buf1.write("Buffer really rocks!")

// Decode a buffer
buf1.toString() // 'Buffer rea'//oops, because buf1 is created to contain only 10 bytes, it couldn't accommodate the rest of the characters// Compare two buffers

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Justice Mba

Written by

Ambitious JavaScript learner, teacher, speaker, writer, developer. ❤️@reactjs @nodejs & friends. Open to new opportunities.

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead