How to Build and Deploy a Deno Application on Koyeb
5 minIntroduction
Deno is a secure by default JavaScript and TypeScript runtime built in Rust and using V8. Deno provides great primitives to ease developers life such as:
- Strong security by default, you have to explicitly enable network, filesystem, or environment access
- Support of TypeScript out of the box
- Ships a self-contained, standalone binary
- No package manager, external modules are imported directly into local modules
In this guide, we will build a simple API in Deno using Oak and deploy to production on Koyeb.
Requirements
To successfully follow and complete this tutorial, you need:
- Docker installed on your machine
- A Koyeb account to deploy and run the dockerized Deno application
Steps
To successfully complete this guide, you need to follow the steps below:
- Write a simple Deno API with Oak
- Dockerize the Deno application
- Push the Docker image on the Docker Hub
- Deploy the Deno application to production on Koyeb
Write a simple Deno API with Oak
Let's start by writing a simple Deno API application with Oak. In a terminal, create a new folder and go to he freshly created directory.
Create a new file name deno-demo.ts
and add the code snippet below:
The code above uses the Deno oak module, which is a middleware framework for Deno’s HTTP server and also provides a router middleware.
The application listens on port 8080
and exposes three different routes:
/
: Returning Hello World! as a response when a request is made/songs
: Returning a list of songs/songs/:id
: Returning details about a specific song
Dockerize the Deno application
The next step is to Dockerize our Deno application to deploy and run it on the Koyeb serverless platform. In the koyeb-deno-example
folder, create a Dockerfile with the content below:
As you can see, we pass the --allow-net
flag in the CMD
instruction. As Deno is secured by default, we need to explicitly enable network access.
Build the Docker image by running the following command in the koyeb-deno-example
folder:
In this guide, we will push the Docker image on the Docker Hub registry. Replace <YOUR_DOCKER_HUB_USERNAME>
with your Docker Hub username.
Once the build done, run the Docker image locally to ensure everything is running fine:
Open your browser and navigate to http://localhost:8080
. You see Hello world!
served by the Docker container.
Push the Docker image on the Docker Hub
As we previously built and tested our Docker image, we can now push it to the Docker Hub. In your terminal run the command below to push the image.
Within a few minutes, you will see your Docker image available on your Docker Hub account repository.
Deploy the Deno application to production on Koyeb
In the Koyeb control panel, on the Overview tab, click click Create Web Service to begin:
- Select Docker as the deployment method.
- Enter the name of the image you created in the Docker image field.
- Click the Private image toggle and, in the select field, click Create secret. In the form that appears, fill in a name for the Secret, for instance
docker-hub-secret
, the registry provider, Docker Hub in our case, your Docker Hub username and password. We recommend you to generate an access token from the Docker Hub to use instead of your password. Once you've filled all the fields, click the Create button. - Choose a name for your App and Service, for example
koyeb-deno-demo
, and click Deploy.
You can add more regions to deploy your applications, set environment variables, and define the horizontal scaling according to your needs.
You will automatically be redirected to the Koyeb App page where you can follow the progress of your Deno application deployment. In a few seconds, once your app is deployed, click on the Public URL ending with koyeb.app
.
Your Deno application is now running on Koyeb and benefits from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network.