🎯 Master Your Career
Interview-Bytes
Expertly curated technical interview questions,
simplified into manageable lessons.
Filter by Topic
Showing 11–11 of 11 questions
11
💡 What is the Buffer class in Node.js?
It is a global object which handles raw binary data in memory (Space is allocated in RAM) allowing us to manipulate raw data streams efficiently
It is great for file I/O and Network communications
Example - Reading a binary file using buffer. fs.readFile reads binary data from file & saves in buffer. Buffer.toString(‘base64’) is used to convert raw binary data to base64
const fs = require('fs');
// Read a binary file (like an image) as a buffer
fs.readFile('image.png', (err, data) => {
if (err) throw err;
// `data` is a Buffer containing the binary content of the file
console.log('Original Buffer:', data);
// Convert the Buffer to a string in base64 encoding
// Useful when we want to send it over a network
const base64Data = data.toString('base64');
console.log('Base64 Encoded Data:', base64Data);
});