Bun: how to host your apps on Clever Cloud

BANNIER bun
Bun on Clever Cloud : discover how to host your apps on Clever Cloud with this tutorial.

For more than a year now, Jarred Sumner and his team have been working on a new open source JavaScript runtime: Bun. It’s touted as more efficient and feature-rich than current solutions such as Node.js or Deno. Version 1.0.3 has just been released, and you can already use it for your projects hosted by Clever Cloud.

While we like to automate and simplify everything for our customers, we do so while leaving as much freedom as possible. Thus, hosting a Node.js application with the framework or package manager of your choice can be done without any particular constraints, and deploying a website takes only a few minutes.

We already support Deno, Meteor.js, yarn. While we haven’t yet taken the decision to integrate Bun natively into our images, as we await initial feedback from our customers, it’s all very straightforward.

Hello, Bun on Clever Cloud!

Want to give it a try? All you need is a Clever Cloud account, or create one from an e-mail address or GitHub account. We’ll give you 20 euros worth of credits when you sign up.

For this first example, we’ll assume that you have a machine running git and a recent version of Node.js. If you haven’t yet installed our open source Clever Tools command line interface (CLI), type (with administrator rights on your system or sudo if necessary) :

npm i -g clever-tools
clever login

Once logged in, you can check that everything has gone correctly with the following command:

clever profile

Let’s install Bun and create our first application:

npm install -g bun
mkdir clevbun && cd clevbun
bun init -y && git init

This will create a local git repository and a whole series of files, including package.json which contains your project’s configuration. But also index.ts and ts.config.json, which are specific to the TypeScript language used by Bun by default. Don’t worry, it handles simple JavaScript files just as well as JSX/TSX, ECMAScript and CommonJS modules, which are imported in a unified way, low-level languages or SQLite. That’s one of its strengths.

Let’s check that everything worked as planned:

> bun index.ts
Hello via Bun!

An application using Bun’s HTTP server

Our aim is to host a web application, so we’re going to use an HTTP server. It’s a good thing that Bun has its own, in addition to ensuring compatibility with Node.js functionalities and APIs. Developed using the Zig language, using the JavaScriptCore engine rather than v8, this runtime claims to be much more efficient for such uses.

Modify the contents of the index.ts file as follows:

Bun.serve({
  port: 8080,
  hostname: '0.0.0.0',
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === "/") return new Response("Home page!");
    if (url.pathname === "/blog") return new Response("Blog!");
    return new Response("404!");
  },
  error(error) {
    return new Response(`<pre>${error}\n${error.stack}</pre>`, {
      headers: {
        "Content-Type": "text/html",
      },
    });
  },
});

This creates an HTTP server that sends a different response depending on the route (url.pathname) and a 404 error if none matches. If a problem occurs, a dedicated page will be displayed. Note that port 8080 is here specified in the code, but it could be defined with an environment variable: $PORT, $BUN_PORT or $NODE_PORT.

Clever deploy!

Now let’s get down to the nitty-gritty of deploying on Clever Cloud, starting by preparing the application:

clever create -t node clevbun
clever env set CC_PRE_BUILD_HOOK "npm i -g bun"
clever env set CC_NODE_BUILD_TOOL "custom"
clever env set CC_CUSTOM_BUILD_TOOL "bun install"
clever env set CC_RUN_COMMAND "bun run index.ts"

The first command creates the application within your Clever Cloud account. Then we define environment variables for installing Bun within the image, followed by instructions for configuring dependencies and launching the project. As with Node.js, you can specify that an application is ready for production by assigning the value production to the NODE_ENV environment variable.

It is then available from our infrastructure and opened in your browser. Note that you can use this configuration to use Bun on a Node.js application already hosted by Clever Cloud by adapting CC_RUN_COMMAND to your project.

Now that our application has been created, it can be deployed. It’s as simple as a git push!

git add . && git commit -m "First commit"
clever deploy && clever open

This should deploy the application on our infrastructure and open it in your browser. You can add /blog or /random at the end of the URL to check that routing works properly.

Many possibilities

You don’t have to worry about configuring HTTPS access, log collection or metrics – everything is already set up, accessible through Clever Tools or the Console. You can also use the latter to activate Grafana in your account settings.

clever console

Other Clever Tools features let you assign it a domain, view its parameters and activity, modify the number and type of instances used, and stop or restart it. Take a look at our CLI help with --help or its documentation to find out more:

clever domain
clever domain add domain.tld

clever status
clever activity

clever scale --min-instances 1 --max-instances 4 --min-flavor pico --max-flavor M
clever scale --instances 1 --flavor pico

clever restart
clever stop

Of course, you can also go further with Bun and deploy applications using the Elysia web framework or static site generators such as Astro. We’ll tell you all about that in future blog posts! Meanwhile, let us know what you expect from our JavaScript runtime images.

Blog

À lire également

MateriaDB KV, Functions: discover the future of Clever Cloud at Devoxx Paris 2024

Clever Cloud is proud to present its new range of serverless products: Materia!
Company

Our new logs interface is available in public beta

You can now discover our new log stack interface and its new features!
Company

Deploy from GitLab or GitHub

Over the past few months, some customers have raised questions about CI/CD building to deploy…

Engineering