Node Js Tutorial
- Node JS Introduction
- Node JS Installation
- Node JS Architecture
- Node JS Modules
- Node Js: FILE SYSTEM MODULES
- EVENTS IN NODE JS
- Node JS and Mysql
- Create Application in Node JS
- Global Object in Node JS
- Creating and loading Node Module
- EXTEND EVENT EMITTER
Questions & Answers
BUFFERS IN NODE JS
Buffers help developers to deal with binary data. In ecosystem that traditionally only deals with strings rather than binaries. Buffers deeply link with streams when the stream processor receives the data faster than it can digest, it puts the data in buffer, Stream in node JS simply means a sequence of data being moved from one point to other point over time. The whole concept is you have a huge amount of data to process but you don’t need to wait for all the data to be available before you start processing it.
You can think of buffer like an array of integers which each represent a byte of data.
Let’s take example with code-:
Take a variable constant “buff” is equal to and call a buffer class here and from this buffer call a method from as-:
Constbuf = buffer.from (“hey”)
So this is kind of string and then after it is as-:
Result is -:
Buffer is like an array so you can access it using square bracket as-:
Now if you execute file by adding this line.
Result is as-:
So as you notice result is 72. As you have three numbers represent this string and this 72 represent this first edge character these numbers and unique code that identifies the character in the buffer position so here’s the buffer.
As you see the result is 101 and 121. So here from “hey”, ‘h’ has unit code 72, ‘e’ has unit code 101 and ‘y’ has unit code 121. Now print this buffer using “tostring” and execute this file.
So as you see above result of “tostring” method, so this buffer is going to print this message using this “tostring” method. As buffer is going to take the data as binaries, now here buffer class is called and call the ‘from’ method of buffer class to create this buffer, now if you want you can write to a buffer a whole string of data by using a “write method”. To get rid of this method, call here the “allocate” method (to allocate a memory for the buffer) as-:
Constbuf = buffer.alloc(4)
And specify 4 using “buffer.write” method as-:
It means you allocate the size of this buffer- 4, so specify here four characters inside this “write method”.
Now save the change and execute-:
So here is the same result, so using this technique, you can also write your own buffer. As you notice you can access the buffer using array index. You are not limited to only access the buffer index. Just like you can access a buffer with array syntax. You can also set the content of the buffer in the same way.
Now let’s add something more. Let’s say-:
Buf and first index 111 which is Equal to “o” this is unique code of ‘o’as-:
Buf = 111 // o and write console after
Now print this buffer as-:
So as you can notice, having ‘o’ at the first index of this buffer, so it’s all how to work with buffer.