Base64 Encoding and Decoding in Node.JS

Use-Cases of this code snippet

Encoding and decoding base64 data in Node.js can be done using the Buffer module.

Node.js does not support the standard Javascript methods of atob() and btoa() for base64 conversions.

Base64 encoding and decoding can be done in Node.js using the Buffer module. This module is loaded by default, hence no import is required.

The Buffer class can be used to manipulate streams of binary data in Node. The Buffer.from() method can create a buffer (binary data) from a given string in a specified encoding. toString() method can then be used on this buffer object to decode it as required.

Encoding to Base64 in Node.js

To convert a string to base64, we need to create a buffer from the given string. This buffer can then be decoded as base64.