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!");

}

Sources:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: