V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bestehen
V2EX  ›  Node.js

node js 运行代码报错

  •  
  •   bestehen · 2018-07-17 12:57:08 +08:00 · 3122 次点击
    这是一个创建于 2081 天前的主题,其中的信息可能已经有所发展或是发生改变。

    function n(t) { return JSON.parse(a("5e5062e82f15fe4ca9d24bc561080e0e",atob(t),0,0,"012345677890123",1)) }

    这种 atob 在 js 里面运行为啥报错呢?如何解决?

    2 条回复    2018-07-17 13:34:34 +08:00
    lizheming
        1
    lizheming  
       2018-07-17 13:11:08 +08:00
    atob 是浏览器端方法,Node 中不存在的,需要使用 Buffer.from 方法转一下

    ```js
    const btoa = str => Buffer.from(str).toString('base64');
    const atob = str => Buffer.from(str, 'base64').toString();
    ```
    via: https://stackoverflow.com/questions/23097928/node-js-btoa-is-not-defined-error/23097961
    bestehen
        2
    bestehen  
    OP
       2018-07-17 13:34:34 +08:00
    @lizheming function n(t) {
    return JSON.parse(a("5e5062e82f15fe4ca9d24bc561080e0e",Buffer.from(t, 'base64').toString(),0,0,"012345677890123",1))
    }
    我改成这样后,还是报错如下
    N = decodeURIComponent(escape(N))
    ^

    URIError: URI malformed
    at decodeURIComponent (native)
    at a (/home/shenjianlin/js/code.js:33:5)
    at n (/home/shenjianlin/js/code.js:48:19)
    at Object.<anonymous> (/home/shenjianlin/js/code.js:55:13)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.runMain (module.js:611:10)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5707 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:47 · PVG 09:47 · LAX 18:47 · JFK 21:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.