1var buffer = Buffer.from('hello');//create a buffer of the text "hello"
2//right now, buffer == <Buffer 68 65 6c 6c 6f>
3var string64 = buffer.toString('base64');
4//the .toString operator can set encoding
5//string64 == 'aGVsbG8='
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"