Getting started with Rust functions in Node.js with VSCode

• 2 minutes to read

In this tutorial, I will show you how to experiment with Rust and Node.js development without installing any developer tools software. Why do I want to write Rust functions in Node.js apps? There are several compelling reasosns. Performance, security, portability, and manageability are among the key reasons.

Fork this GitHub repository to get started. In your fork, you can use GitHub's web UI to edit source code files.

  • The Rust files are in the src directory. You can put high performance workload into Rust functions. The Rust build and dependency configuration is in the Cargo.toml file.
  • The JavaScript files are in the node directory and they can access the Rust functions. The node/app.js file contains the application.

VS Codespaces steps

VS Codespaces runs entirely in your browser and costs around $1 per work day. It is cheaper than a cup of coffee in the office. Alternatively, in steps 1-2 below, you could use locally installed VSCode and Docker, and launch the IDE with your remote git repository.

First, open the VSCode Codespaces web site and login with your Azure account. You can get a free Azure account.

Next, create a new codespace. Put your forked repository into the Git Repository field. This step takes a few minutes. But once a codespace is created, subsequent openings only take seconds.

create a new codespace

Open the source code files and make changes as you wish!

develop

Click on the Run button on the left panel, and then the Launch Program at the top to build and run the application.

run

The Terminal window at the bottom shows the build progress. It builds the Rust program, and then launches the Node.js app.

build

The Debug window shows the Node.js server running and waiting for web requests.

server

Now, open another terminal window in the IDE via the Terminal -> New Terminal menu.

test

From the terminal window, you can test the local server.

$ curl http://127.0.0.1:3000/?name=SSVM
hello SSVM

In fact, you can run any Linux command from VSCode's built-in Terminal. You could run ssvmup build to build, and then node node/app.js to run the Node.js application. The Node.js application could be a server application as we have shown here, or simply a command line program as many of our later examples.

terminal

That's it! VSCode has many useful features such as real time error detection and syntax highlighting as you type code, advanced Github integration, and integrations with many many development tools. Next, head over to this guide to learn the Rust and JavaScript code in this example.

Enjoy coding!

RustWebAssemblyNode.jsVSCodegetting-startedrust-function-in-nodejs
Fast, safe, portable and serverless Rust functions as services