在 node.js 中,Buffer.from() 方法抛出 "ascii is not a function" 错误

In node.js, Buffer.from() method is throwing "ascii is not a function" error

在Node.js中,缓冲区class是全局的。当我试图在 Visual Studio 代码中执行下面的代码行时,node.js 抛出异常。

我的代码

var buffer = Buffer.from('Hello','ascii');

异常

TypeError: ascii is not a function
    at Function.from (native)
    at Function.from (native)

您看到的 Buffer.from() 实际上是 ArrayBuffer.from(),它具有不同的行为。对于 LTS 用户来说幸运的是,node v4.5.0 最近发布了 包含新节点 Buffer API(包括自定义 Buffer.from())。此外,节点 v6.x(将在 10 月成为下一个 LTS 分支)已经拥有这些 API 一段时间了。