How to Encode / Decode to / from base64 in Node.JS

Today I ran into this basic problem. A quick search on google gave me the answer, I tested it, it worked, and the rest is history.

Here is the code.

let plainText = "Hello World!";

console.log("Plaint text = " + plainText);

// Encoding to base64

let base64 = new Buffer(plainText).toString('base64');

console.log("Encoded to base64 = " + base64);

// Decoding from base64

let ascii = new Buffer(base64, 'base64').toString('ascii');

console.log("Decoded from base64 = " + ascii);

if (plainText === ascii) {

console.log("Everything went well!");

} else {

console.log("Something went wrong!");



Leave a Comment

Your email address will not be published. Required fields are marked *