How to do Base64 encoding in node.js?

前端 未结 7 1290
渐次进展
渐次进展 2020-11-22 16:46

Does node.js have built-in base64 encoding yet?

The reason why I ask this is that final() from crypto can only output hex, binary or ascii

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 17:19

    Buffers can be used for taking a string or piece of data and doing base64 encoding of the result. For example:

    You can install Buffer via npm like :- npm i buffer --save

    you can use this in your js file like this :-

    var buffer = require('buffer/').Buffer;
    
    ->> console.log(buffer.from("Hello Vishal Thakur").toString('base64'));
    SGVsbG8gVmlzaGFsIFRoYWt1cg==  // Result
    
    ->> console.log(buffer.from("SGVsbG8gVmlzaGFsIFRoYWt1cg==", 'base64').toString('ascii'))
    Hello Vishal Thakur   // Result
    

提交回复
热议问题