Node.js throws “btoa is not defined” error

后端 未结 10 1580
悲&欢浪女
悲&欢浪女 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:41

    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();
      }
    };
    

提交回复
热议问题