Buffers are used in Node.js to handle binary data. They store raw data in an array format which can be converted into various other formats like JSON, String, Array, etc.
Here’s a general example of how to use Buffers in Node.js:
```
// Create a new buffer containing the word ‘Hello‘
let buffer = Buffer.from(‘Hello’);
console.log(buffer);
// Logs:
console.log(buffer.toString());
// Logs: Hello
// Create a new buffer of length 10 and add values to it.
let buffer2 = Buffer.alloc(10);
buffer20 = 2;
buffer21 = 255;
console.log(buffer20);
// Logs: 2
console.log(buffer21);
// Logs: 255
```
Buffer.from() method is used to create a new buffer with a string or an array. In the above code lines, Buffer.from(‘Hello’) creates a new buffer having the binary representation of ‘Hello’ in UTF-8 encoding.
Buffer.alloc() method is used to create a new buffer with specified length.
In Node.js applications, Buffer is mainly used to read images or audio files, or to get raw data from files.
To understand more about the methods available with Buffer class: https://nodejs.org/api/buffer.html