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.

Anshu
  • 1,277
  • 2
  • 13
  • 28
Arnab Roy
  • 301
  • 1
  • 3
  • 5
  • 1
    this article may help: https://stackabuse.com/encoding-and-decoding-base64-strings-in-node-js/ – uminder Jul 11 '19 at 06:41

3 Answers3

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)
});
MKesper
  • 456
  • 5
  • 16
John
  • 4,786
  • 8
  • 35
  • 44
26

Please Use btoa for encode string

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

use for atob decode the same string

console.log(atob("YWJj")); // abc
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Yugma Patel
  • 529
  • 3
  • 9
  • 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 '19 at 05: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 08 '20 at 08:55
  • 7
    @deprecated — Use Buffer.from(data, 'base64') instead. – Mo Zaatar Jan 17 '22 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);
waz
  • 1,165
  • 16
  • 31
  • 2
    There really is no reason to use a 3rd party package. – zerkms Dec 11 '19 at 23:08
  • @zerkms If you're not using a 3rd party package, how do you do it? – waz Dec 12 '19 at 23:28
  • 4
    `Buffer.from(b64, 'base64').toString()` – zerkms Dec 12 '19 at 23:36
  • You're right. However I had to pass it through twice. I'll update my answer – waz Dec 13 '19 at 01: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 '19 at 01:44
  • My use case is to upload the image to AWS S3, and every time I upload anything but what I have done, it doesn't actually work. I am note sure why. I'm as confused as you.. – waz Dec 13 '19 at 02:14
  • Because you're decoding it twice. – zerkms Dec 13 '19 at 02:36