How To Make An Express Server With Bun.

I've been using Node for a long while and express almost goes hand and hand. It's the go-to http server with countless middleware packages. Let's use it in Bun.

You can read all about Bun here bun.sh.

Now to get started:

  • Install Bun:
curl -fsSL https://bun.sh/install | bash
  • Create a new project. I personally did npm init to create a package.json file.
mkdir bun-express
cd bun-express
npm init
  • We'll need just one dependency:
bun add express
  • Create an index.js file and add the following code:
import express from "express";

// Initialize the express app
const app = express();

// Hello World GET endpoint
app.get("/", (req, res) => {
  res.send("Hello World!");
});

// Set the default port to 3000, or use the PORT environment variable
const port = process.env.PORT || 3000;

app.listen(port, () => console.log(`Express 🥟 Server Listening on port ${port}`));
  • Run the server:
bun index.js

You should see something like this in the console:

Express 🥟 Server Listening on port 3000
  • Let's deploy this app using a Dockerfile. Create a new file called Dockerfile and add the following code:
FROM jarredsumner/bun:0.5.1
WORKDIR /app
COPY package.json package.json
COPY bun.lockb bun.lockb
RUN bun install
COPY index.js index.js
EXPOSE 3000
ENTRYPOINT ["bun", "index.js"]

You can test the Dockerfile locally if you have Docker installed by running the following command:

docker build -t bun-express .

docker run -p 3000:3000 bun-express
  • Commit and push up the code to a new repository. I suggest adding node_modules/ to the .gitignore file.

  • I deployed using Render, but there are tons of options to deploy an application in a Docker Container.

I selected "New +" and then selected "Web Service". Once I connected my repository to the service, it detected my Dockerfile and was building and deploying in minutes.

To test that deployment works, visit the Express server on the newly deployed endpoint. You should see "Hello World!"

Congrats, if everything worked you have an Express server running on Bun in the cloud!

You can find the source code for my example Express Server w/ Bun on GitHub here. The code is going to look a bit different than the example here in this blog because I opted to put my index.js into a /src folder.