30

I have an express server written in typescript.

As atob() or btoa() works on browsers, on Nodejs.

We generally use

Buffer.from("some-string").toString('base64') to encode string to base64.

However, this doesn't seem to work when I am writing the code in TypeScript. I need some help with this.

1

3 Answers 3

39

in Node typescript:

import { Buffer } from "buffer";

const b64 = "SGVsbG8sIFdvcmxkIQ==";
const str = 'Hello, World!'

const decode = (str: string):string => Buffer.from(str, 'base64').toString('binary');
const encode = (str: string):string => Buffer.from(str, 'binary').toString('base64');

test('base64 decode', () => {
  expect(decode(b64)).toEqual(str)
});

test('base64 decode', () => {
  expect(encode(str)).toEqual(b64)
});

test('base64 encode/decode', () => {
  expect(decode(encode(str))).toEqual(str)
});
26

Please Use btoa for encode string

console.log(btoa("abc")); // YWJj

use for atob decode the same string

console.log(atob("YWJj")); // abc

3
  • 10
    atob() or btoa() is throwing an error as btoa is not defined. I guess this works if you are hosting this on a browser. However, as a REST server, this might not work.
    – Arnab Roy
    Jul 11, 2019 at 5:47
  • 1
    I understand the issue with whether or not this runs in a browser. My solution is browser-based and this worked really nicely. Thank you.
    – K-Dawg
    Oct 8, 2020 at 8:55
  • 7
    @deprecated — Use Buffer.from(data, 'base64') instead.
    – Mo Zaatar
    Jan 17, 2022 at 23:46
3

If you have used window.btoa(fileData) on the front end.

NOTE: After the feedback from zerkms and also reading the package code, it seems that you can just do it manually. However I had to run it twice to work.
I was also trying to decode a large image.

Then on the nodejs server you can use Buffer directly:

const b64 = "SGVsbG8sIFdvcmxkIQ==";
const fileDataProcessed = Buffer.from(b64, 'base64').toString('binary')
const decodedData = Buffer(fileDataProcessed, 'base64')

// This is the code that you can now upload to your s3 bucket, or somewhere else.
console.log(decodedData);
7
  • 2
    There really is no reason to use a 3rd party package.
    – zerkms
    Dec 11, 2019 at 23:08
  • @zerkms If you're not using a 3rd party package, how do you do it?
    – waz
    Dec 12, 2019 at 23:28
  • 4
    Buffer.from(b64, 'base64').toString()
    – zerkms
    Dec 12, 2019 at 23:36
  • You're right. However I had to pass it through twice. I'll update my answer
    – waz
    Dec 13, 2019 at 1:28
  • I'm really confused - if you need a string you can do Buffer.from(b64, 'base64').toString('binary'). If you need a buffer - you do Buffer.from(b64, 'base64'). What's the point of const decodedData = Buffer(fileDataProcessed, 'base64')? It produces garbage 🤷
    – zerkms
    Dec 13, 2019 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.