Simple rust script to achieve a bridge between node.js and rust. Use with the npm package rust-bridge.
Add the following line to your Cargo.toml under [dependencies]
toml
node_bridge = "0.1.0"
Node.js installation:
bash
$ npm install rust-bridge
```rust
async fn main() { nodebridge::register(vec![("add", add), ("findlonger", find_longer)]).await; // Results if the bridge closes. }
fn add(params: Vec<&str>) -> String {
(params[0].parse::
fn findlonger(params: Vec<&str>) -> String { if params[0].len() > params[1].len() { return params[0].tostring(); } params[1].to_string() }
Handling in node.js:
javascript
import RustBridge from "rust-bridge";
const bridge = new RustBridge("/path/to/rust-executable"); await bridge.init();
console.log(await bridge.add(1,2)); // "3"
console.log(await bridge.find_longer("long", "longer")); // "longer"
bridge.close(); // true ```
Contributions are always welcome!