nodejs不支持使用atob和btoa 进行字符串base64转换
/** * string转为base64 */ function stringToBase64(str) { return new Buffer.from(str).toString("base64"); } /** * base64转字符串 */ function base64ToString(b64) { return new Buffer.from(b64, "base64").toString(); }
示例
// 转为base64 var str1 = "aaabbbccc"; console.log(stringToBase64(str1)); // base64反解析为字符串 var str2 = "YWFhYmJiY2Nj"; console.log(base64ToString(str2));