nodejs 0.1.1

Embedding Node.js in Rust
Documentation

rust-nodejs

Embedding Node.js in Rust.

  • Provide a global thread-safe Node.js event queue.
  • Interact with the Node.js runtime via Neon API.
  • Link with prebuilt Node.js binaries to save compile time.
  • Native modules are supported.

Usage

  1. Add rust-nodejs to your cargo project:
[dependencies]
nodejs = "0.1.1"
  1. let queue = nodejs::event_queue() to get the global Node.js event queue.
  2. Call queue.send to run tasks in the Node.js event queue
  3. Inside the task, use nodejs::neon for interoperability between Node.js and Rust. Neon documentation
  4. On macOS or Linux, add -Clink-args=-rdynamic to rustflags when building your Rust application.

Example

use nodejs::neon::{context::Context, types::JsNumber, reflect::eval};

fn main() {
    let (tx, rx) = std::sync::mpsc::sync_channel::<i64>(0);
    let queue = nodejs::event_queue();
    queue.send(move |mut cx| {
        let script = cx.string("require('os').freemem()");
        let free_mem = eval(&mut cx, script)?;
        let free_mem = free_mem.downcast_or_throw::<JsNumber, _>(&mut cx)?;
        tx.send(free_mem.value(&mut cx) as i64).unwrap();
        Ok(())
    });
    let free_mem = rx.recv().unwrap();
    println!("Free system memory: {}", free_mem);
}