How Base64 Encoding and Decoding is Done in Node.js ?
Last Updated :
08 Jan, 2025
Base64 encoding and decoding are when you need to encode binary data into a text format that can be easily transmitted over text-based protocols like HTTP. Node.js provides built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class.
Approach
To perform Base64 encoding and decoding in Node.js, use the Buffer class. Encode data with Buffer.from(data).toString('base64'), and decode with Buffer.from(base64String, 'base64').toString(). This approach works for both strings and binary data.
Encoding the original string to base64
The Buffer class in Node.js can be used to convert a string to a series of bytes. This can be done using Buffer.from() method that accepts the string to be converted and the current encoding of the string. This encoding can be specified as "utf8". The converted bytes can then be returned as a base64 using the toString() method. This method accepts a parameter that specifies the encoding needed during conversion. In this case, "base64" is specified as the encoding to be used. Thus, this method converts any string to the base64 format.
Syntax:
// Create buffer object, specifying utf8 as encoding
let bufferObj = Buffer.from(originalString, "utf8");
// Encode the Buffer as a base64 string
let base64String = bufferObj.toString("base64");
Example: This example demonstrates how to convert a UTF-8 string into a Base64 encoded string using Node.js Buffer.
JavaScript
// The original utf8 string
let originalString = "GeeksforGeeks";
// Create buffer object, specifying utf8 as encoding
let bufferObj = Buffer.from(originalString, "utf8");
// Encode the Buffer as a base64 string
let base64String = bufferObj.toString("base64");
console.log("The encoded base64 string is:", base64String);
Output:
The encoded base64 string is: R2Vla3Nmb3JHZWVrcw==
Decoding base64 to original string
The Buffer can also be used to convert the base64 string back to utf8 encoding. The Buffer.from() method is again used to convert the base64 string back to bytes, however, this time specifying the current encoding as "base64". The converted bytes can then be returned as the original utf8 string using the toString() method. In this case, "utf8" is specified as the encoding to be used. Thus, this method converts the base64 to its original utf9 format.
Syntax:
// Create a buffer from the string
let bufferObj = Buffer.from(base64string, "base64");
// Encode the Buffer as a utf8 string
let decodedString = bufferObj.toString("utf8");
Example: This example shows how to decode a Base64 encoded string back into its original UTF-8 string using Node.js Buffer.
JavaScript
// The base64 encoded input string
let base64string = "R2Vla3Nmb3JHZWVrcw==";
// Create a buffer from the string
let bufferObj = Buffer.from(base64string, "base64");
// Encode the Buffer as a utf8 string
let decodedString = bufferObj.toString("utf8");
console.log("The decoded string:", decodedString);
Output:
The decoded string: GeeksforGeeks
Conclusion
Base64 encoding and decoding in Node.js are straightforward with the Buffer class. This capability is particularly useful for handling binary data that needs to be transmitted over text-based protocols or stored in text formats. Whether you're dealing with strings, images, or other binary files, Node.js makes it easy to encode and decode Base64 data efficiently.
Similar Reads
Encoding and Decoding Base64 Strings in Python The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss about Base64 encoding and decoding and its uses to enco
4 min read
How to convert blob to base64 encoding using JavaScript ? Blob in JavaScript is a fundamental data type representing bytes of data. It serves as the underlying structure for files and is supported by web browsers, capable of storing and retrieving data from memory. Blobs have specific sizes and file types like regular files and can be handled with Buffers,
2 min read
JavaScript | Encode/Decode a string to Base64 To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a-
6 min read
How to Convert JSON to base64 in JavaScript ? Base 64 is the encoding scheme that represents binary data in a printable ASCII format, commonly used for data serialization and transmission. Table of Content Using btoa functionUsing Manual ConversionUsing btoa functionIn this approach, we're using btoa to encode a UTF-8 string representation of a
2 min read
How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim
2 min read
Encrypting Data in Node.js Encryption and Decryption in Node can be done by installing and implementing the 'crypto' library. If you have installed Node.js by manual build, then there is a chance that the crypto library is not shipped with it. You can run the following command to install the crypto dependency. npm install cry
1 min read