Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode/encode string to base64 in typescript express server

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.

like image 931
Arnab Roy Avatar asked Jul 09 '19 12:07

Arnab Roy


People also ask

How do I generate base64-encoded string in typescript?

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

How do I decode a base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

How do I decode a base64 string in node?

Base64 encoding and decoding can be done in Node. js using the Buffer object. Encoding the original string to base64: The Buffer class in Node. js can be used to convert a string to a series of bytes.

How do you convert a string to base64?

To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.


1 Answers

Please Use btoa for encode string

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

use for atob decode the same string

console.log(atob("YWJj")); // abc
like image 123
Yugma Patel Avatar answered Oct 26 '22 04:10

Yugma Patel