Site icon ParallelCodes

Node.js – Base64 Encoding and Decoding using Buffer

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');

console.log(base64Val);
console.log(decodedVal);

Output:

VG9kYXkgaXMgU2F0dXJkYXksIDE1dGggTWF5IDIwMjE=
Today is Saturday, 15th May 2021

About Base64:


What is Base64 format?

Base64Encode.io – 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:

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.


Exit mobile version