Node.js throws “btoa is not defined” error

后端 未结 10 1568
悲&欢浪女
悲&欢浪女 2020-12-02 04:40

In my node.js application I did an npm install btoa-atob so that I could use the btoa() and atob() functions which are native in clien

10条回答
  •  隐瞒了意图╮
    2020-12-02 04:56

    The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.

    If you need to convert to Base64 you could do so using Buffer:

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

    Reverse (assuming the content you're decoding is a utf8 string):

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

    Note: prior to Node v4, use new Buffer rather than Buffer.from.

提交回复
热议问题