Node.js – Base64 Encoding and Decoding using Buffer

  • by

Base64 Encoding and Decoding in Node.js:

Buffer in Node.js can be used for encoding string into base64 value and also to decode into string.

Syntax for Encoding string to base64 value:

let base64Val = Buffer.from(value).toString('base64');

Syntax for Decoding base64 value to string object:

let decodedVal = Buffer.from(base64Val, 'base64').toString('ascii');

Example of Encoding to base64 and Decoding from base64:

let plainText = "Today is Saturday, 15th May 2021";

let base64Val = Buffer.from(plainText).toString('base64');
let decodedVal = Buffer.from(base64Val, 'base64').toString('ascii');



Today is Saturday, 15th May 2021

About Base64:

What is Base64 format? – A simple web tool to help you in encoding your string data into base64 format and decode base64 data into plain text.

Base64 is used to represent binary data in an ASCII string format by translating it into a radix-64 representation. Each Base64 digit represents exactly 6 bits of data. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit base64 digits.

Users of Base64 Encoding:

Base64 format is commonly used in:

  • Sending attachments while Emailing. Attachments are converted into base64 format.
  • Sending media files like Images, audio files through a Web API.
  • Saving data in base64 format inside a database.
  • Transfer data using a URL.

Security constraints:

As Base64 encoding is not a secure encryption process and anyone can easily decode it, we should never really rely only on Base64 encoding for our applications.

Also see:
Node.js – How to create folder or directory

Node.js – Create, read and write text files.

HTTP module in Node.js

Install Node.js on Windows.

Node.js – How to create PDF documents.

  1. - Realtime Encode your string to Base64 format.
  2. - Realtime Decode Base64 data to plain text.
  3. - Realtime Encoding in Md5Hash.