Languages
[Edit]
EN

Node.js - atob / btoa functions equivalents

4 points
Created by:
Jan-Alfaro
681

In this short article, we would like to present equivalents for btoa() and atob() functions under Node.js.

Quick solutions:

const btoa = (text) => Buffer.from(text, 'binary').toString('base64');
const atob = (base64) => Buffer.from(base64, 'base64').toString('binary');

 

Practical example

btoa() and atob() are common JavaScript functions, but they are not available under Node.js, so it is necessary to use some equivalents.

 

btoa() function equivalent

"btoa" should be read as "binary to ASCII". btoa() function converts binary to Base64-encoded ASCII string.

const btoa = (text) => {
    const buffer = Buffer.from(text, 'binary');
    return buffer.toString('base64');
};


// Usage example:

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

 

atob() function equivalent

"atob" should be read as "ASCII to binary". atob() function converts Base64-encoded ASCII string to binary.

const atob = (base64) => {
    const buffer = Buffer.from(base64, 'base64');
    return buffer.toString('binary');
};


// Usage example:

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

 

See also:

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

References

  1. Buffer - Node.js Docs

Alternative titles

  1. atob function in Node.js
  2. btoa function in Node.js
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