1> console.log(Buffer.from("Hello World").toString('base64'));
2SGVsbG8gV29ybGQ=
3> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
4Hello World
1//--------------- HOW TO ENCODE BASE64 ON NODEJS ----------------------
2//create a buffer of the text "Hello World"
3var buffer = Buffer.from('Hello World');
4//buffer result is: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
5
6var string64 = buffer.toString('base64');
7// .toString('base64') allow to encode it to base64
8// result is: SGVsbG8gV29ybGQ=
9
10// Can be use combined together like these
11console.log(Buffer.from('Hello World').toString('base64'));
12// result is: SGVsbG8gV29ybGQ=
1//--------------- HOW TO DECODE BASE64 ON NODEJS ----------------------
2//create a buffer of the text "Hello World"
3var buffer = Buffer.from('SGVsbG8gV29ybGQ=', 'base64');
4//buffer result is: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
5
6var string64 = buffer.toString('base64');
7// .toString('ascii') allow to decode base64
8// result is: "Hello World"
9
10// Can be use combined together like these
11console.log(Buffer.from('SGVsbG8gV29ybGQ=', 'base64').toString('ascii'));
12// result is: "Hello World"
13
1'use strict';
2
3let data = 'stackabuse.com';
4let buff = new Buffer(data);
5let base64data = buff.toString('base64');
6
7console.log('"' + data + '" converted to Base64 is "' + base64data + '"');
8