Node.js Buffers

Node.js gives Buffer class to store raw data like a array of numbers yet compares to a raw allocation portion outside the V8 pile. Buffer class is utilized on the grounds that pure JavaScript isn't pleasant to paired information. Along these lines, when managing TCP streams or the document framework, it's important to deal with octet streams.

Buffer class is a global class. It very well may be gotten to in application without bringing in Buffer module.

Node.js Creating Buffers

There are numerous approaches to build a Node buffer. Following are the three for the most part utilized methods:

  1. Create a uninitiated buffer: Following is the language structure of making a uninitiated support of 10 octets:
    1. var buf = new Buffer(10);  
  2. Create a support from exhibit: Following is the language structure to make a Buffer from a given array:
    1. var buf = new Buffer([10, 20, 30, 40, 50]);   
  3. Create a Buffer from string: Following is the syntax structure to make a Buffer from a given string and alternatively encoding type:
    1. var buf = new Buffer("Simply Easy Learning", "utf-8");   

Node.js Writing to buffers

Following is the strategy to compose into a Node buffer:


  1. buf.write(string[, offset][, length][, encoding])  

Parameter explanation:

string: It determines the string data to be composed to buffer.

offset: It indicates the file of the Buffer to begin composing at. Its default esteem is 0.

length: It determines the quantity of bytes to compose. Defaults to buffer.length

encoding: Encoding to utilize. 'utf8' is the default encoding.

Return values from composing buffers:

This technique is utilized to return number of octets composed. On case of room lack for support to fit the whole string, it will compose a part of the string.

Let's take an example:

Create a JavaScript document named "main.js" having the accompanying code:

File: main.js

  1. buf = new Buffer(256);  
  2. len = buf.write("Simply Easy Learning");  
  3. console.log("Octets written : "+  len);  

Open the Node.js order brief and execute the accompanying code:

Following is the technique to peruse data from a Node buffer.


  1. buf.toString([encoding][, start][, end])  

Node.js Reading from buffers

Following is the method to read data from a Node buffer.


  1. buf.toString([encoding][, start][, end])   Parameter explanation:

    encoding: It specifies encoding to use. 'utf8' is the default encoding

    start: It specifies beginning index to start reading, defaults to 0.

    end: It specifies end index to end reading, defaults is complete buffer.

    Return values reading from buffers:

    This method decodes and returns a string from buffer data encoded using the specified character set encoding.

    Let's take an example:

    File: main.js

    1. buf = new Buffer(26);  
    2. for (var i = 0 ; i < 26 ; i++) {  
    3.   buf[i] = i + 97;  
    4. }  
    5. console.log( buf.toString('ascii'));       // outputs: abcdefghijklmnopqrstuvwxyz  
    6. console.log( buf.toString('ascii',0,5));   // outputs: abcde  
    7. console.log( buf.toString('utf8',0,5));    // outputs: abcde  
    8. console.log( buf.toString(undefined,0,5)); // encoding defaults to 'utf8', outputs abcde  

    Open Node.js command prompt and execute the following code:

    1. node main.js  

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.