Buffers are Node.js' built-in type for storing arbitrary binary data. Because most Node.js developers don't use buffers much beyond occasionally reading data from a file, buffers are a common source of confusion. In this article, I'll demonstrate how buffers work in Node.js, and describe a neat use case for buffers with MongoDB and Mongoose.
Working With Buffers
Buffer class is a global variable in Node.js. Here's how you create a Buffer with 1 byte using the Node.js shell.
$ node -v v8.9.4 $ node > Buffer.alloc(1) <Buffer 00> >
Node.js introduced the
... read the whole story at thecodebarbarian.com.
Buffer.alloc() function in v5.10.0, so the above code won't work in older versions of Node.js. The above buffer isn't very useful, it just contains a single '0' byte. Let's create a more interesting buffer using the