Languages
[Edit]
EN

Node.js - atob / btoa functions equivalents

0 points
Created by:
Jan-Alfaro
281

btoa() and atob() are common JavaScript functions, but are not included in Node.js. Below you will find the equivalents of these functions 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.

btoa function equivalent

// "btoa" should be read as "binary to ASCII"
// btoa converts binary to Base64-encoded ASCII string
const btoa = (text) => {
    return Buffer.from(text, 'binary').toString('base64');
};

console.log(btoa('This is text...')); // VGhpcyBpcyB0ZXh0Li4u

atob function equivalent

// "atob" should be read as "ASCII to binary"
// atob converts Base64-encoded ASCII string to binary
const atob = (base64) => {
    return Buffer.from(base64, 'base64').toString('binary');
};

console.log(atob('VGhpcyBpcyB0ZXh0Li4u')); // This is text...

See also:

  1. Node.js - base64 with Unicode support
  2. Node.js - encoding / decoding base64

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