Take your first Deno app online in under five minutes
Background image by Leone Venter on Unsplash.
Deno has made its way into the developer community, and unlike its predecessor (which took some time to take off), it is making waves. The internet is already filled with articles comparing it with Node.js, with some even saying that Node is dead for good. Well, I don't want to jump into that discussion, but Deno is here to stay.
Even though it will take a while for people to start using it in production and for popular cloud providers to start providing a runtime for Deno out of the box, that doesn't mean we can’t start experimenting with all the new features. So here’s an article explaining how to deploy a Deno app to a Heroku project. Let’s jump in.
1. Create Your Deno App
In this project, we are going to use a very simple Deno app that contains a single get
API that returns a set of sample to-dos. The code for server.ts
is very much boilerplate, but let’s go through the lines that are relevant to a Heroku project:
- Line 3: We are importing a command-line argument parser for Deno that we will use to pass the port number.
- Line 7: We store the arguments passed during runtime from the CL in this constant.
- Line 8: We define a default port in case our argument is not defined.
- Lines 9-10: We retrieve the port value from the arguments passed along with the Deno command, check that it’s not undefined, and assign it to the port variable used for our app.
The rest is pretty straightforward. If you are confused about the port variable, please hang on. It will make sense in a moment.
2. Create a Heroku App
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It’s absolutely free to use. Since this is a Heroku tutorial, I will start with the basics. You may skip to the last section if you already know how to connect a Heroku app.
- Create an account on Heroku.
- Install the Heroku CLI on your system if you haven’t already done it.
- Run
heroku login
from your terminal to authorize your Heroku account. - Run
heroku create <app-name>
from the terminal inside your project folder. This will create a Heroku app and connect it to a Heroku git remote. - This is probably the most important command in this tutorial. After creating the app, run
heroku buildpacks:set [https://github.com/chibat/heroku-buildpack-deno.git](https://github.com/chibat/heroku-buildpack-deno.git)
in your command line.
In the last step, we configured our app to use a third-party buildpack for Deno, as an official Deno buildpack is not available in Heroku yet.
3. Create a Procfile
Now in the root of your project, create a file named Procfile
without any extensions. This is a file that specifies the commands that are executed by the Heroku app on startup. Add this single line of code in your Procfile:
web: deno run --allow-net=:${PORT} --cached-only server.ts --port=${PORT}
Here, along with the Deno run
command, we are adding the PORT
variable that will contain the dynamic port exposed by Heroku dynos for your app to bind to.
This is very important since Heroku doesn't allow us to define ports in the code.
This is what the sample project structure looks like.
4. Push to Heroku
Now your app is configured to run on Heroku and all you have to do is commit all the files and run git push heroku master
from your command line. Upon successful deployment, you will see a message like this in your command line:
Deploying the Heroku app
Now just navigate to the Heroku app URL given at the end of the message, followed by your API endpoint, and you will be able to see your response. In case you wish to monitor your app’s live logs, you can do so from your command line. Just open a new command-line instance and run the command heroku logs — tail
.
Conclusion
You can find the full source code in the GitHub repository. Special thanks to Chibat for creating the Heroku Deno buildpack.