DEV Community

Cover image for High performance Node.js static file serving — using Rust
Marcus Kohlberg for Encore

Posted on

9 5 5 5 5

High performance Node.js static file serving — using Rust

A few months ago we released Encore.ts — an Open Source backend framework for TypeScript / Node.js.

Since there are already many frameworks out there, we want to share some of the outlier design decisions we've made and how they lead to improved performance and developer experience.

Serving static files in Node.js, using Rust for high performance

Encore.ts comes with built-in support for serving static assets (such as images, HTML and CSS files, and JavaScript files).

This is useful when you want to serve a static website or a single-page application (SPA) that has been pre-compiled into static files.

When defining static files, the files are served directly from the Encore.ts Rust Runtime.

This means that no JavaScript code is executed to serve the files, freeing up the Node.js runtime to focus on executing business logic.

Encore.ts rust runtime alongside node.js

This dramatically speeds up both the static file serving, as well as improving the latency of your API endpoints.

We recently published benchmarks showing how it is 9x faster than Express.js based Node applications.

Learn more in the docs

Example app showing how to serve static files with Encore.ts

Code example

Serving static files in Encore.ts works similar to have you define API endpoints, but using the api.static function instead of the normal api function.



import { api } from "encore.dev/api";

export const assets = api.static(
  { expose: true, path: "/frontend/*path", dir: "./assets" },
);


Enter fullscreen mode Exit fullscreen mode

This will serve all files in the ./assets directory under the /frontend path prefix.

Encore automatically serves index.html files at the root of a directory. In the case above, that means that requesting the URL /frontend will serve the file ./assets/index.html, and /frontend/hello will serve the file ./assets/hello or ./assets/hello/index.html (whichever exists).

Wrapping up

By using a Rust based runtime alongside Node.js, Encore.ts is able to improve performance of TypeScript applications.

If performance matters to your project, it might be a good idea to try out Encore.ts.

And it's all Open Source, so you can check out the code and contribute on GitHub.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (2)

Collapse
 
dreama profile image
Dream

This is really interesting! How difficult was the process of integrating Rust with Node.js, especially in terms of developer ergonomics?

Collapse
 
marcuskohlberg profile image
Marcus Kohlberg

It took quite a while... :)
Regarding ergonomics, are you asking for the end user? Then.. not at all. It is fully transparent and you only write TypeScript, no Rust.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay