Crate nodejs[−][src]
Expand description
Embedding Node.js in Rust.
- Queue tasks to the Node.js event loop thread-safely.
- Interact with the Node.js runtime via Neon API.
- Link with prebuilt Node.js binaries to save compile time.
- Native modules are supported.
Guide
- Copy the .cargo folder in this repo to your cargo project to enable flags required to link Node.js properly.
let channel = nodejs::channel()
to get the global Node.js channel.- Call
channel.send
to run tasks in the Node.js event queue. - Inside the task, use
nodejs::neon
for interoperability between Node.js and Rust. Neon documentation
Example
fn main() { let (tx, rx) = std::sync::mpsc::sync_channel::<String>(0); let channel = nodejs::channel(); channel.send(move |mut cx| { use nodejs::neon::{context::Context, reflect::eval, types::JsString}; let script = cx.string("require('http').STATUS_CODES[418]"); let whoami = eval(&mut cx, script)?; let whoami = whoami.downcast_or_throw::<JsString, _>(&mut cx)?; tx.send(whoami.value(&mut cx)).unwrap(); Ok(()) }); let whoami = rx.recv().unwrap(); assert_eq!(whoami, "I'm a Teapot"); }
Re-exports
pub use neon;
Functions
Ensure the Node.js runtime is running and return the channel for queuing tasks into the Node.js event loop.