Node.js Buffer Tutorial
In this post, we will give you information about Node.js Buffer Tutorial. Here we will give you detail about Node.js Buffer Tutorial And how to use it also give you a demo for it if it is necessary.
Node.js buffers are objects that store arbitary binary data. The most common reason for running into buffers is reading files using Node.js:
const fs = require('fs');
const buf = fs.readFileSync('./package.json');
buf instanceof Buffer; // true
buf; // '<Buffer 7b 0a 20 20 22 6e 61 6d 65 22 ...>'
Buffers have a toString()
function that takes a single argument encoding
. The toString()
function lets you convert buffers into meaningful strings depending on encoding. For example, if you read an ordinary text file using fs.readFile()
, you can
convert the buffer into the text from the file using .toString('utf8')
:
const fs = require('fs');
const buf = fs.readFileSync('./package.json');
buf.toString('utf8'); // '{ "name": "onlinecode.org", ...}'
Another common encoding is hex
, which encodes the buffer as a string of characters [0-9A-F]
. Hex encoding is useful because it doesn’t
require escaping – you can put a hex encoded buffer into a URI without using encodeURIComponent()
or put it into JSON without escaping "
, because hex encoding only contains alphanumeric characters.
const fs = require('fs');
const buf = fs.readFileSync('./package.json');
buf.toString('hex'); // '7b0a2020...'
Creating a New Buffer forĀ Node.js Buffer Tutorial
You can create buffers from strings using the Buffer.from()
function. Like toString()
, you can pass an encoding
argument to Buffer.from()
.
let buf = Buffer.from('Hello, World', 'utf8');
buf.toString('hex'); // '48656c6c6f2c20576f726c64'
buf.toString('utf8'); // 'Hello, World'
buf = Buffer.from('48656c6c6f2c20576f726c64', 'hex');
buf.toString('utf8'); // 'Hello, World'
The Buffer.from()
function also accepts arrays and buffers. You can
use Buffer.from()
to clone a buffer:
const buf2 = Buffer.from(buf);
buf2 === buf; // false
buf2.toString('utf8'); // 'Hello, World'
Or from an array of numeric bytes:
const buf = Buffer.from([
0x48,
0x65,
0x6c,
0x6c,
0x6f,
0x2c,
0x20,
0x57,
0x6f,
0x72,
0x6c,
0x64
]);
buf.toString('utf8'); // Hello, World
With JSON.stringify()
The JSON.stringify()
function converts buffers into objects. The raw data is encoded as an array of bytes that you can pass in to Buffer.from()
.
let buf = Buffer.from('Hello, World', 'utf8');
let obj = { buffer: buf };
obj = JSON.parse(JSON.stringify(obj));
// { type: 'Buffer',
// data: [ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100 ] }
obj.buffer;
// To convert from JSON representation back to a buffer, use 'Buffer.from()'
obj.buffer = Buffer.from(obj.buffer);
obj.buffer.toString('utf8'); // 'Hello, World'
More Node Tutorials
- Working with UUID in Node
- Using bcrypt-js to Hash Passwords in JavaScript
- Working with the Node.js assert Library
- Modify Authorized redirect_uris For Google OAuth
- Sleep in NodeJS
- Convert HTML to Pug
- Convert Pug to HTML
Node.js is an open-source, cross-platform JavaScript runtime environment. Node.js allows you to run JavaScript code outside of a web browser. This makes it possible to create a wide variety of applications, including web servers, real-time chat applications, and data processing pipelines.
Node.js is built on top of the V8 JavaScript engine, which is also used by Google Chrome. This means that Node.js applications can take advantage of the same performance and features as Chrome.
Node.js is a popular choice for building web servers. This is because Node.js is very efficient at handling concurrent requests. Node.js uses a non-blocking event loop to handle requests, which means that it can handle multiple requests at the same time without slowing down.
Node.js is also a good choice for building real-time chat applications. This is because Node.js is able to push data to clients in real time without the need for polling. This makes it possible to create a smooth and responsive user experience.
Node.js is a powerful tool that can be used to create a wide variety of applications. If you are looking for a JavaScript runtime environment that is fast, scalable, and efficient, then Node.js is a great choice.
Here are some of the benefits of using Node.js:
- Performance: Node.js is very efficient at handling concurrent requests. This makes it a good choice for building web servers and real-time chat applications.
- Scalability: Node.js is designed to scale horizontally. This means that you can add more nodes to your cluster to handle more traffic.
- Efficiency: Node.js uses a non-blocking event loop to handle requests. This makes it possible to handle multiple requests at the same time without slowing down.
- Community: Node.js has a large and active community. This means that there are many resources available to help you learn and use Node.js.
Hope this code and post will helped you for implement Node.js Buffer Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs