从 node.js Web应用中调用 WASM 函数

文中所有的代码都可以在 https://github.com/second-state/wasm-learning/tree/master/nodejs/hello 中找到


从 node.js Web应用中调用 WASM 函数 | WebAssembly 入门教程


在之前的教程中,我们讨论了如何从 Web 浏览器中的 JavaScript 应用程序访问 WebAssembly 函数。

在《 》一文中,介绍了WebAssembly 在服务端的用例,尤其是人工智能、区块链和大数据应用方向。

在这个例子中,将展示如何将 Rust 中编写的 WebAssembly 函数集成到服务器上的 node.js 应用程序中。

我们以微服务的方式提供 WebAssembly 函数。

演示应用程序的结构如下:

  • 主机应用程序是一个用 JavaScript 编写的 node.js web 应用程序,调用 WebAssembly 函数
  • WebAssembly 字节码程序是用 Rust 编写的,由 node.js 的 web 应用程序调用。

设置

与前面的教程一样,我们使用 wasm-pack 工具编译 Rust 源代码并生成相应的 JavaScript 模块。这种模块使得在 JavaScript 和 Rust 函数之间传递复杂的动态数据变得非常容易。想要深入了解的同学,可以阅读《 》。

接下来,按照下面的步骤安装Rust和wasm-pack 工具。

<code># Install Rust
$ sudo apt-get update
$ sudo apt-get -y upgrade
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env

# Install wasm-pack tools
$ curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
/<code>

Rust 写的 WebAssembly 程序

在这个示例中,Rust 程序在“hello” 后面追加输入字符串。

创建一个新的cargo项目。

注意:由于这个程序是从主机应用程序调用的,而不是作为独立的可执行文件运行,因此我们将创建一个 hello 项目。

<code>···
$ cargo new --lib hello
$ cd hello
/<code>

编辑 Cargo.toml 文件添加 [lib] 部分. 它告诉编译器在哪能找到库的源代码,以及如何生成字节码输出。同时,我们也需要在这添加 wasm-bindgen 的依赖项。这是使用 wasm-pack的用途,生成绑定 JavaScript 的 rust webassembly程序

<code>[lib]
name = "hello_lib"
path = "src/lib.rs"
crate-type =["cdylib"]
[dependencies]

wasm-bindgen = "0.2.50"
/<code>

下面是Rust 程序 src/lib.rs 的内容。实际上,我们可以在这个库文件中定义多个外部函数,并且所有这些函数都可以通过 WebAssembly 在主机 JavaScript 应用程序中使用。

<code>use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn say(s: String) -> String {
let r = String::from("hello ");
return r + &s;
}
/<code>

接下来,您可以将 Rust 源代码编译成 WebAssembly 字节码,并为 node.js 主机环境生成相应的 JavaScript 模块。

<code>$ wasm-pack build --target nodejs
/<code>

结果是以下三个文件. .wasm 文件是 WebAssembly 字节码程序; .js 文件用于 JavaScript 模块;

<code>Pkg / hello lib bg. wasm
Pkg / hello lib bg. js
Pkg / hello lib.js
/<code>

Node.Js 主机应用程序

接下来,让我们为 node.js web 应用程序创建一个节点文件夹,复制生成的 JavaScript 模块文件。

<code>$mkdir node
$cp pkg / hello lib bg. wasm node /
$cp pkg / hello lib bg. js node /
$cp pkg / hello lib.js node /
/<code>

使用生成的 hello_lib.js, 在 JavaScript 中调用 WebAssembly 函数是非常容易的。

下面是节点应用程序 app.js。 它只是从生成的模块中导入 say () 函数。 节点应用程序从传入的 httpget 请求中获取 name 参数,并用“ hello name”进行响应。

<code>const { say } = require('./hello_lib.js');

const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 8080;

const server = http.createServer((req, res) => {
const queryObject = url.parse(req.url,true).query;
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(say(queryObject['name']));
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
/<code>

按照以下方式启动 node.js 应用服务器。

<code>$ node app.js
Server running at http://127.0.0.1:8080/
/<code>

然后,就可以测试了。

<code>$ curl http://127.0.0.1:8080/?name=Wasm
hello Wasm
/<code>

下一步是什么呢?

现在 Web 服务可以将计算量大、不安全和新颖的硬件访问任务转移到 WebAssembly 中。 我相信,将有更多用例将会出现。 敬请期待!


分享到:


相關文章: