"Base64 encoding/decoding in node. js"
Here's how you encode normal text to base64 in node. JS:
var=newBuffer(' JavaScript ');
var= b. ToString(' base64 ');
smf2yvnjcmlwda==
And here's how you decode Base64 encoded strings:
var=newBuffer(' smf2yvnjcmlwda== ',' base64 ')
var= b. ToString();
Javascript
If you is interested in the details of how the above examples worked, follow me.
The new Buffer()
constructor requires a number, array or string as the first parameter, and an optional encoding type as the Seco nd parameter. The possible encoding types is ASCII, UTF8, UCS2, base64, binary, and hex; The default being UTF8.
By passing the second parameter, we tell JavaScript, the "the string you see was encoded in this particular format". Notice how we do that in the decoding example.
Once we have the encoded string and we call the toString()
method on the string. If we don ' t pass the encoding type toString()
to, JavaScript assumes we want to convert the object to UTF8 encoded string by de Fault. We can make it convert to other formats by passing the encoding type to toString()
.
Reference: http://www.hacksparrow.com/base64-encoding-decoding-in-node-js.html
Base64 encoding/decoding in node. js