What is Buffer in Node.js?

A buffer is an area of memory. JavaScript developers are not familiar with this concept as C, C++ developers have. It represents a fixed-size chunk of memory allocated outside of the V8 JavaScript engine. It helps developers to deal with binary data. Buffer in node.js is not related to the concept of buffering data.

Buffer can be created using :

Buffer.form():

const buf= Buffer.form(“Hello”)

Buffer.alloc():

const buf= Buffer.alloc(1024)

Buffer.allocUnsafe()

const buf= Buffer.allocUnsafe(1024)

You can write to a buffer a whole string of data by using the write() method:

buf.write(string,offset,length,encoding)

The buf.toString() method is used to read the data from a node buffer:

buf.toString(encoding, start, end)