Node.js不能解码string..字符乱码(问号)

我真的希望这只是工作,但…

我得到一个base64string的头…我想编码为UTF8。

strInit = req.headers['authorization'] buf = new Buffer(strInit.length) i = 0 while i < strInit.length buf[i] = strInit.charCodeAt(i) i++ str = buf.toString() str2 = new Buffer(str, 'base64').toString() console.log("AUTH REQUEST :",strInit, buf, str, str2) AUTH REQUEST : Basic dXNlckBnbWFpbC5jb206cXdlcnR5 <Buffer 42 61 73 69 63 20 64 58 4e 6c 63 6b 42 6e 62 57 46 70 62 43 35 6a 62 32 30 36 63 58 64 6c 63 6e 52 35> Basic dXNlckBnbWFpbC5jb206cXdlcnR5  "q ͕            ݕ   

我尝试在线解码,并按预期显示(user@gmail.com:qwerty)

例如在这里它工作正常: http : //www.base64decode.org

我在想什么?

解决:好吧,其实我发现它…我不得不从string中删除“基本”,所以解码器不会感到困惑。

所以解决scheme只是:

 new Buffer(req.headers['authorization'].replace("Basic ",""),"base64").toString() 

这样它的工作。