The JavaScript language itself has only a string data type and no binary data type. However, when processing a TCP stream or file stream, you must use the binary data. So in node. js, a buffer class is defined that creates a buffer dedicated to binary data that allows node. js to process binary data and, whenever it needs to process data moved in I/O operations in node. JS, it is possible to use the buffer library, a Buffer is similar to an array of integers, but it corresponds to a piece of raw memory outside of the V8 heap memory.
Create a Buffer object (3 ways)
var New // Create a Buffer instance with a length of 10 bytes
var New // creates a Buffer instance from the given array
var New // using a string to create a Buffer instance, Utf-8 is the default encoding, with other encodings "ASCII", "UTF8", "Utf16le", "UCS2", "base64", and "hex".
Write buffers (2 ways)
var New Buffer (buf.write); ("HelloWorld");
var New Buffer (+); for (var i = 0; i <; i++= i + +);} Console.log (buf.tostring (/// output: abcdefghijklmnopqrstuvwxyz//default = UTF8 optional)
Returns a JSON object.
var New Buffer (' www.runoob.com '); var json =// output for [119, 119, 119, 111, 111, 98, 117, A. A, a.
Buffer consolidation
var New Buffer (' rookie tutorial '); var New Buffer (' www.runoob.com '); var buffer3 = Buffer.concat ([Buffer1,buffer2]); Console.log (// output is Buffer3 content: Rookie Tutorial Www.runoob.com
The buffer of node. js