在我的node.js应用程序中,我做了一个npm安装btoa-atob,这样我就可以使用btoa()和atob()函数,这些函数在客户端javascript中是原生的,但由于某种原因没有包含在node中。新目录出现在我的node_modules文件夹中,它本身和app.js一起在根目录下。然后我确保在包中添加btoa-atob作为依赖项。Json文件,在根目录下。

然而,由于某种原因,它仍然不能工作。

console.log(btoa("Hello World!"));

^应该输出“SGVsbG8gV29ybGQh”到控制台,但是相反,我得到了错误:

Btoa没有定义。

我没有正确地安装吗?我忽略了什么?


当前回答

export const universalBtoa = str => {
  try {
    return btoa(str);
  } catch (err) {
    return Buffer.from(str).toString('base64');
  }
};

export const universalAtob = b64Encoded => {
  try {
    return atob(b64Encoded);
  } catch (err) {
    return Buffer.from(b64Encoded, 'base64').toString();
  }
};

其他回答

我有一个服务器和客户端之间共享的代码,我需要一个btoa的实现在它里面。 我试着这样做:

const btoaImplementation =  btoa || (str => Buffer.from(str).toString('base64'));

但服务器会崩溃:

ReferenceError: btoa没有定义

而Buffer没有在客户端上定义。

我不能检查窗户。Btoa(这是一个共享代码,记得吗?)

所以我最终得到了这个实现:

const btoaImplementation = str => {
    try {
        return btoa(str);
    } catch(err) {
        return Buffer.from(str).toString('base64')
    }
};

'btoa-atob'模块不导出编程接口,它只提供命令行实用程序。

如果你需要转换为Base64,你可以使用Buffer:

console.log(Buffer.from('Hello World!').toString('base64'));

反向(假设你解码的内容是utf8字符串):

console.log(Buffer.from(b64Encoded, 'base64').toString());

注意:在Node v4之前,使用new Buffer而不是Buffer.from。

Atom编辑器中的“script”插件也有同样的问题,这是一个旧版本的node,没有btoa(),也没有atob(),也不支持Buffer数据类型。下面的代码可以做到这一点:

var Base64 = new function() { var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" this.encode = function(input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = Base64._utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4); } return output; } this.decode = function(input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = keyStr.indexOf(input.charAt(i++)); enc2 = keyStr.indexOf(input.charAt(i++)); enc3 = keyStr.indexOf(input.charAt(i++)); enc4 = keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = Base64._utf8_decode(output); return output; } this._utf8_encode = function(string) { string = string.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; } this._utf8_decode = function(utftext) { var string = ""; var i = 0; var c = 0, c1 = 0, c2 = 0, c3 = 0; while (i < utftext.length) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i + 1); c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } }() var btoa = Base64.encode; var atob = Base64.decode; console.log("btoa('A') = " + btoa('A')); console.log("btoa('QQ==') = " + atob('QQ==')); console.log("btoa('B') = " + btoa('B')); console.log("btoa('Qg==') = " + atob('Qg=='));

下面是一个简洁的base64编码通用解决方案:

const nodeBtoa = (b) => Buffer.from(b).toString('base64');
export const base64encode = typeof btoa !== 'undefined' ? btoa : nodeBtoa;

我能够使用btoa的二进制数据到基于64字符串转换使用下面的npm包: https://www.npmjs.com/package/btoa

正如他们的文档中所描述的,我在node JS应用程序中做了以下步骤:

Install => npm Install——保存btoa Declare at top => const btoa = require('btoa'); 使用=> const b64 = btoa("stringToEncode");