Javascript

How to deploy a NodeJS application using Fly.io

Matthew Pagan
4 min read
How to deploy a NodeJS application using Fly.io
This article was written over 18 months ago and may contain information that is out of date. Some content may still be relevant, but please refer to official documentation for the latest information.

I was astounded when I learned just how easy it is to deploy static web applications. Services like AWS Amplify, Netlify, GitHub Pages, etc, trivialize this process, allowing developers to focus less on deployment, and more on feature development.

I was equally astounded when I learned that it can be JUST AS EASY to deploy a web service! There are many options out there:

This guide is going to be focused on a solution that I have found to be incredibly developer-friendly platform, Fly.io.

What is Fly.io?

Fly.io Home Page

Deploy App Servers Close to Your Users

Run your full-stack apps (and databases!) all over the world. No ops required.

Fly.io claims to be able to run full-stack apps with no ops required. That's a bold statement. While I've committed to plenty of PR's, I haven't deployed a web service in a while. I was feeling the itch to deploy a service to play around with some different languages in the backend, so I was 100% in the market for a developer-friendly way to get this done, with quite literally no ops hopefully required.

Fly.io App Deployment Speed-run

The last time I personally deployed a brand new web service was months and months ago, on a DigitalOcean droplet. My usual go-to solution for web apps are static web apps, so I'm used to deploying static assets.

Fly.io has a particular portion of their documentation which immediately drew my attention: their Speedrun section.

Fly.io Speedrun

Before deciding to write this blog, I figured, I'd at least try out their service, and if I enjoyed it, I'd write about it. Holy moly was I shocked just how easy Fly.io made deployment. After about 15 minutes from landing on their documentation, I was able to deploy a web service, complete with GitHub Actions CI/CD. And I didn't even need to touch a server!

Here is how you can do the same.

I created an incredibly terse ExpressJS app using TypeScript.

Sample Express App

  1. Install flyctl: The root URL for Fly.io has a one line command you can use to install flyctl, the command line tool which handles deployment for you. For detailed instructions though, check out this page.
  2. Sign up / Log in: Sign up or Log in using the appropriate command, flyctl auth signup or flyctl auth login.
  3. Run flyctl launch: This will prompt you to name your application, select a region to deploy to, and then deploy your app. This generates a fly.toml config file with the deployment configurations. Fly Launch

In those three steps, you too can launch a sample express app.

Successfully deployed

CI/CD

Adding CI/CD is as simple as adding a single file to the repo, along with retrieving and adding an API token to the repo.

Add the following to .github/workflows/main.yml

name: Fly Deploy
on: [push]
env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
  deploy:
    name: Deploy app
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: superfly/flyctl-actions@1.1
        with:
          args: "deploy"

Then run flyctl auth token to generate and print an API token, then add it to the repo as a new secret named FLY_API_TOKEN.

That is all you need to continuously deploy your NodeJS application on every commit!

CICD

Conclusion

Using Fly.io honestly felt like cheating. I remember being super proud of myself when I first deployed a full LAMP stack application on a VPS. I also clearly remember the pain of troubleshooting things like SSH, CertBot and LetsEncrypt, Nginx, etc.

Fly.io completely trivialized the entire process. I easily see myself using this going forward for all containerized applications since the service even includes the means for auto-scaling your apps and containers!!!

If you are a developer and you are not shy of the command-line, I highly recommend checking out Fly.io and spending 15 minutes of your time, just deploying a service. You'll be shocked at just how quick and easy it can be to go from 0 to CI/CD in mere moments.

About the author

Matthew Pagan

Matthew Pagan

Software Engineer

Keep reading

View all posts →

Vercel vs. VPS - What's the drama, and which one should you choose?

A breakdown of the hosting options you have and when you should choose a cloud provider such as Vercel and when a VPS or a Linux server in your basement is just enough....

Jan Kaiser8 mins
DevOpsNodeJSJavaScriptVercel

Testing a Fastify app with the NodeJS test runner

ant to simplify your testing process? Our new blog post on Node.js' built-in test runner is a great place to start. Learn how to test Fastify apps, including practical examples for testing an API server and SQL plugins....

Dane Grant5 mins
NodeJSTypeScriptTesting

Bun v1.0

On September 8, 2023, Bun version 1 was released as the first production ready version of Bun, a fast, all in one toolkit for running, building, testing, and debugging JavaScript and TypeScript. Why a new JS runtime You may ask, we already have Node and Deno, so why would we need another javascript runtime, Well yes we had Node for a very long time, but developers face a lot of problems with it, and maybe the first problem is because it’s there for a very long time, it has been changing a lot between different versions and one of the biggest nightmares for JavaScript developers these days is upgrading the node version. Also, Node lacks support for Typescriptt. Zig programming language One of the main reasons that Bun is faster than Node, is the programming language it has been built with which is Zig. Zig is a very fast programming language, even faster than C) (here is some benchmarks), it focuses on performance and memory control. The reasons it’s faster than C is because of the LLVM optimizations it has, and also the way it handles the undefined behavior under the hood Developer Experience Bun delivers a better developer experience than Node on many levels. First, it’s almost fully compatible with Node so you can use Node packages without any issues. Also, you don’t need to worry about JS Common and ES Modules anymore, you can use both in the same file, yup you read that right, for example: Also, it has a built in test framework similar to Jest or Vitest in the project so no need to install a different test framework with different bundler in the same project like Webpack or Vite Also, it supports JSX out of the box Also, Bun has the fastest javascript package manager and the most efficient you can find as of the time of this post Bun Native APIs Bun supports the Node APIs but also they have fun and easy APIs to work with like Bun.serve() : to create HTTP server Bun.file() : to read and write the file system Bun. password.hash(): to hash passwords Bun.build(): to bundle files for the browser Bun.FileSystemRouter(): a file system router And many more features Plugin system Bun also has an amazing plugin system that allows developers to create their own plugins and add them to the Bun ecosystem. Conclusion Bun is a very promising project, and it’s still in the early stages, but it has a lot of potential to be the next big thing in the JavaScript world. It’s fast, easy to use, and has a lot of amazing features. I’m very excited to see what the future holds for Bun and I’m sure it will be a very successful project....

Mark Shenouda2 mins
BunJavaScriptnpmNodeJS

How to automatically deploy your full-stack JavaScript app with AWS CodePipeline

In our previous blog post we set up a horizontally scalable deployment for our full-stack javascript app. In this article we would like to show you how to set up AWS CodePipeline to automatically deploy changes to the application....

Balázs Tápai9 mins
AWSNxNodeJSJavaScript