Build, Deploy and Run a Node Application

Getting an application running on Fly is essentially working out how to package it as a deployable image. Once packaged it can be deployed to the Fly infrastructure to run on the global application platform. For this Getting Started article, we'll look at building a Node application from scratch.

The Hellonode Application

Our example will be a basic "hello world" example using Node and Express.

You can get the code for the example from the hellonode Github repository. Just git clone https://github.com/fly-examples/hellonode to get a local copy. Here's all the code:

const express = require("express");
const app = express();
const port = process.env.PORT || 3000;

app.get(["/", "/:name"], (req, res) => {
  greeting = "<h1>Hello From Node on Fly!</h1>";
  name = req.params["name"];
  if (name) {
    res.send(greeting + "</br>and hello to " + name);
  } else {
    res.send(greeting);
  }
});

app.listen(port, () => console.log(`HelloNode app listening on port ${port}!`));

We'll call this file server.js and run npm init and npm install express --save so we've got the basic node setup.

Running The Application

Run node server.js to start the application

node hellonode.js          
HelloNode app listening on port 3000!

And connect to localhost:3000 to confirm that you have a working Node application. Now to package it up for Fly.

Install Flyctl and Login

We are ready to start working with Fly and that means we need flyctl, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to our installation guide. Once thats installed you'll want to login to Fly.

Configuring the App for Fly

Each Fly application needs a fly.toml file to tell the system how we'd like to deploy it. That file can be automatically generated with the command flyctl init command.

flyctl init
? App Name (leave blank to use an auto-generated name) hellonode

? Select organization: Demo (demo)

? Select builder: node
    Nodejs builtin

? Select Internal Port: 8080

New app created
  Name     = hellonode
  Owner    = demo
  Version  = 0
  Status   =
  Hostname = <empty>

Wrote config file fly.toml

You'll be asked for an application name first. We recommend that you go with the autogenerated names for apps to avoid namespace collisions. We're using hellonode here so you can easily spot it in configuration files.

Next you'll be prompted for an organization. Organizations are a way of sharing applications between Fly users. When you are asked to select an organization, there should be one with your account name; this is your personal organization. Select that.

Flyctl also asks you to select a builder. Builders are responsible for constructing the Docker image of your application which is then deployed to Fly's Firecracker VMs. The simplest to use are the builtin builders, which we recommend you use here. Select Node (Nodejs Builtin). If you want to know more about the various builders, see Builders and Fly.

The last thing you will be asked for an internal port value. This is the port your application communicates over. If set incorrectly, Fly will be unable to connect to the application and it will fail health checks and be terminated. The default shown, 8080, is correct for the builtin builders which, by design, get applications to talk on port 8080. So, hit return and carry on.

Inside fly.toml

The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, "hellonode". If we look at the fly.toml file we can see the name in there:

app = "hellonode"

[build]
  builtin = "node"

[[services]]
  internal_port = 8080
  protocol = "tcp"

  [services.concurrency]
    hard_limit = 25
    soft_limit = 20

  [[services.ports]]
    handlers = ["http"]
    port = "80"

  [[services.ports]]
    handlers = ["tls", "http"]
    port = "443"

  [[services.tcp_checks]]
    interval = 10000
    timeout = 2000

The flyctl command will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Fly platform. The rest of the file contains settings to be applied to the application when it deploys.

We'll have more details about these properties as we progress, but for now, it's enough to say that they mostly configure which ports the application will be visible on.

Deploying to Fly

We are now ready to deploy our app to the Fly platform. At the command line, just run:

flyctl deploy

This will lookup our fly.toml file, and get the app name hellonode from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it's done.

Viewing the Deployed App

Now the application has been deployed, let's find out more about its deployment. The command flyctl info will give you all the essential details.

flyctl status
App
  Name     = hellonode
  Owner    = dj
  Version  = 0
  Status   = running
  Hostname = hellonode.fly.dev

Allocations
ID       VERSION REGION DESIRED STATUS  HEALTH CHECKS      RESTARTS CREATED
ada41989 0       fra    run     running 1 total, 1 passing 0        33s ago

Connecting to the App

The quickest way to browse your newly deployed application is with the flyctl open command.

flyctl open
Opening http://hellonode.fly.dev/

Your browser will be sent to the displayed URL. Fly will auto-upgrade this URL to an HTTPS secured URL.

Bonus Points

  • When our Dockerfile is run, it copies everything from the directory over to the Docker image. For Node applications, some directories like node_modules are going to be rebuilt anyway so there's no need to copy them. Create a .dockerignore file and add node_modules to it to do this.
  • You can also use the .dockerignore to skip unused project assets and any other files which aren't needed at runtime.
  • If you want to know what IP addresses the app is using, try flyctl ips list:
flyctl ips list
TYPE ADDRESS                              CREATED AT
v4   50.31.246.73                         23m42s ago
v6   2a09:8280:1:3949:7ac8:fe55:d8ad:6b6f 23m42s ago

Arrived at Destination

You have successfully built, deployed, and connected to your first Node application on Fly.