How to do Base64 encoding in node.js?

前端 未结 7 1287
渐次进展
渐次进展 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:11

    I am using following code to decode base64 string in node API nodejs version 10.7.0

    let data = 'c3RhY2thYnVzZS5jb20=';  // Base64 string
    let buff = new Buffer(data, 'base64');  //Buffer
    let text = buff.toString('ascii');  //this is the data type that you want your Base64 data to convert to
    console.log('"' + data + '" converted from Base64 to ASCII is "' + text + '"'); 
    

    Please don't try to run above code in console of the browser, won't work. Put the code in server side files of nodejs. I am using above line code in API development.

提交回复
热议问题