Everyone who worked in Node.js, should have came across the term Buffer. Few may think that Buffer is only for library developers and its not in the scope of application developer. Here this blog is for you to provide an overview about the Buffer and its usage.
Everyone who worked in Node.js, should have came across the term Buffer. Few may think that Buffer is only for library developers and its not in the scope of application developer. Here this blog is for you to provide an overview about the Buffer and its usage.
In Node.js, Buffer objects are used to represent the binary data, which is used for data transmission in Node.js. Usage of Buffer is supported in Streams, File system and other Node.js internal API's. To interact with operating system (OS) using Node.js File API, the interactions were happens as a binary data which OS generally understand.
Common usage Buffer Object
Listing out some the common usage of Buffer object
Can I convert binary data (Buffer) as a String ?
To convert Buffer as a String, Buffer uses character encoding for the conversion. Default character encoding is utf8, which also supports options for utf16le, latin1 and others.
In simple words, Once the buffer is processed to String it is encoded to the human readable string format.
Hands on with Buffer
To create a new buffer instance require the buffer global object and create instance using
Buffer.from() to decode the buffer useBuffer.toString(). Following snippet illustrates the examples for creating Buffer instance with different character encoding and with decoding.const buffer = require('buffer').Buffer;
// Encoding : Creates a Buffer containing default utf8 string
let encBuffDefaultUtf8 = buffer.from("hello world");
console.log(encBuffDefaultUtf8); // Output: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
// Decoding : Covert a Buffer to utf8 string.
let decBuffDefaultUtf8 = encBuffDefaultUtf8.toString()
console.log(decBuffDefaultUtf8); // Output: hello world
// Encoding : Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
const encBuffLatin = Buffer.from('tést', 'latin1');
console.log(encBuffLatin) // <Buffer 74 e9 73 74>
// Decoding : Covert a Buffer to Latin-1 string.
const decBuffLatin = encBuffLatin.toString('latin1');
console.log(decBuffLatin) // tést
The usage of Buffer in *https/https API *returns the Buffer object chunk (partial data) on each event and its stored into an Array. Once the stream ends Buffer object is concatenated using
Buffer.concat()
and then parsed back to JSON
javascript buffer nodejs web-development backend memory-management coding software-development
To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.
Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...
Want to create a backend for a web or mobile app using PHP & JS frameworks? **[Hire Dedicated Backend Developers](https://hourlydeveloper.io/hire-back-end-developer/ "Hire Dedicated Backend Developers")** who offer end-to-end, robust, scalable...
Are you looking to hire experienced Backend Developers at a reasonable cost to boost-up your IT business? **[Hire Backend Developers India](https://hourlydeveloper.io/hire-back-end-developer/ "Hire Backend Developers India")** and accomplish...
In the 1930s, the Toyota Production System gave us lean manufacturing principles. Now, the IT, software and web development industry have also adopted these principles to improve their production processes. In actuality, the concepts and principles of Lean are used in more ways than just in manufacturing. Yet, in IT and software, there are still those who are pointing towards Agile development when they mention Lean and software development in the same context. While it is true that Agile and Lean principles share similar philosophies, there are key differences which set them apart. Diving deep inside Lean, I will discuss what lean talks about other than it’s key points.