In my previous article, we looked at how to Base64 encode and decode a string in Java as well as in JavaScript. Today, you'll learn how to do the Base64 encoding and decoding in a Node.js application.

Unfortunately, Node.js doesn't support standard JavaScript functions like atob() and btoa() for Base64 encoding. These methods are part of the window object and only available in the browser.

Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding. Buffer is available as a global object which means that you don't need to explicitly require this module in your application.

Internally, Buffer represents binary data in the form of a sequence of bytes. The Buffer object provides several methods to perform different encoding and decoding conversions. This includes to and from UTF-8, UCS2, Base64, ASCII, UTF-16, and even HEX encoding scheme.

Let us look at the below examples that explain how to use the Buffer object to perform Base64 encoding and decoding in a Node.js application.

Base64 Encoding

To convert a string into a Base64 encoded string, we need to first create a buffer from the given string by using the Buffer.from() method. This method takes two parameters, a plain-text string, and the character encoding, and creates a buffer or binary data array for the given encoding. If no character encoding is specified, UTF-8 will be used as the default.

Here is an example:

// plain-text string
const str = 'Base64 Encoding in Node.js';

// create a buffer
const buff = Buffer.from(str, 'utf-8');

// decode buffer as Base64
const base64 = buff.toString('base64');

// print Base64 string
console.log(base64);

// QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=

In the above example, we created a buffer from the string, and the used the toString() method to decode the buffer as a Base64 string. The second parameter in Buffer.from() is optional when you are dealing with plain-text (UTF-8) strings.

Base64 Decoding

The Base64 decoding process is very much similar to the encoding process. All you need to do is create a buffer from the Base64 encoding string by using base64 as the second parameter to Buffer.from() and then decode it to the UTF-8 string by using the toString() method.

Here is how it looks like:

// Base64 encoded string
const base64 = 'QmFzZTY0IEVuY29kaW5nIGluIE5vZGUuanM=';

// create a buffer
const buff = Buffer.from(base64, 'base64');

// decode buffer as UTF-8
const str = buff.toString('utf-8');

// print normal string
console.log(str);

// Base64 Encoding in Node.js

Conclusion

That's all folks for Base64 encoding and decoding in Node.js. We looked at how to use the native Buffer module to perform the Base64 encoding and decoding in a Node.js application. The Buffer object is not just limited to Base64 conversions. You can even use it to perform ASCII, HEX, UTF-16, and UCS2 encodings and decodings.

If you want to learn more about Base64 conversions in JavaScript, take a look at this guide.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also Subscribe to RSS Feed.

Last Updated: