bestehen
V2EX  ›  Node.js

node js 运行代码报错

  •  
  •   bestehen · Jul 17, 2018 · 3705 views
    This topic created in 2882 days ago, the information mentioned may be changed or developed.

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

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

    2 replies    2018-07-17 13:34:34 +08:00
    lizheming
        1
    lizheming  
       Jul 17, 2018
    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
       Jul 17, 2018
    @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)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1563 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    ♥ Do have faith in what you're doing.