Languages
[Edit]
EN

Node.js - encoding / decoding base64

3 points
Created by:
GamerGoals22
364

In this short article, we would like to show how to encode text to base64 and back (decode base64) in Node.js.

Note:

In the presented solution, we use the Buffer class, which has a global scope in Node.js and does not need to be imported using the require.

const Base64 = {
    encode: (text) => {
        const buffer = Buffer.from(text, 'binary');
        return buffer.toString('base64');
    },
    decode: (base64) => {
        const buffer = Buffer.from(base64, 'base64');
        return buffer.toString('binary');
    },
};

// Example:

console.log(Base64.encode('This is text...'));      // VGhpcyBpcyB0ZXh0Li4u
console.log(Base64.decode('VGhpcyBpcyB0ZXh0Li4u')); // This is text...

See also:

  1. JavaScript - base64 with Unicode support
  2. Node.js - atob / btoa functions equivalents
  3. Node.js - base64 with Unicode support

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - base64

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join