Getting started: Global Deno apps

Table of contents

Deno is a secure runtime for JavaScript and TypeScript built with V8. This guide explains how to deploy a Deno app to Fly.

Build a Deno app

Install Deno

If you haven't installed Deno locally yet, follow the installation instructions for your operating system.

Create a server.ts file

Copy this code to a file named server.ts:

import { serve } from "https://deno.land/std@v0.22.0/http/server.ts";

const { PORT = "8080" } = Deno.env();

console.log("listening on", PORT);

async function helloServer(){
  for await (const req of serve(`:${PORT}`)) {
    const enc = new TextEncoder();
    const body = `Hello from Deno!`;
    req.respond({ body: enc.encode(body) });
    console.log(`${req.method} ${req.url}`)
  }
};

helloServer();

Try running it locally with the Deno command:

deno run server.ts --allow-net --allow-env

If it's working, you should be able to open http://localhost:8080 in your browser and see a polite greeting, and the console logs will look a little like this:

listening on 8080
GET /
GET /favicon.ico
GET /another-url
GET /favicon.ico
GET /another-url/again

Install flyctl and login

We have a CLI tool for deploying and managing Fly applications. If you're on a Mac, you can install the CLI with Homebrew:

brew install superfly/tap/flyctl

For other operating systems, use the install script:

curl https://get.fly.io/flyctl.sh | sh

Once installed, log in to Fly with flyctl auth login:

flyctl auth login
Opening browser to url https://fly.io/app/auth/cli/token
Waiting for session...⣽ 

This will open a browser window you can use to register and authenticate with Fly.

Deploy your code

Create a new Fly app

The flyctl apps create command creates all the necessary infrastructure to deploy your application, and then writes a fly.toml file to your working directory.

flyctl apps create --builder deno
? App Name (leave blank to use an auto-generated name) 
? Select organization: Fly (fly)
New app created
  Name     = deno-deno-deno 
  Owner    = fly             
  Version  = 0               
  Status   =                 
  Hostname = 

Wrote config file fly.toml

This creates a fly.toml file, and sets the builder to deno. By default, this will use the latest version of Deno and try to run the file named server.ts. If you want to customize the Deno environment, you can update fly.toml:

app = "deno-deno-deno"
[build]
  builder = "github.com/superfly/builders/deno"
  entry_file = "./server.ts"
  deno_version = "v0.22.0"
flyctl deploy
Deploy source directory '.'
Docker daemon unavailable, performing remote build...
...
[2019-08-22T18:22:04+00:00] ✅ Successfully built fly-app
[2019-08-22T18:22:04+00:00] /bin/fly-app
...

==> Creating Release
Release v19 created
==> Monitoring Deployment
You can detach the terminal anytime without stopping the deployment

  Version            = v19                                
  Reason             = release                            
  Description        = Deploy image                       
  Status             = succeeded                          
  Status Description = Deployment completed successfully  
  User               = mrkurt@gmail.com                   
  Date               = 16s ago                            

  NAME   PROMOTED   DESIRED   CANARIES   PLACED   HEALTHY   UNHEALTHY   PROGRESS DEADLINE  
  app    true       1         1          1        1         0           just now           

See it in action

Now try running flyctl status, you'll see output like this:

flyctl info
App
  Name     = deno-deno-deno                  
  Owner    = deno                            
  Version  = 19                              
  Status   = running                         
  Hostname = https://deno-deno-deno.fly.dev  

Services
  TASK   PROTOCOL   PORTS  

IP Addresses
  TYPE   ADDRESS                                CREATED AT            
  v4     77.83.140.104                          2019-08-22T18:20:45Z  
  v6     2a09:8280:1:2fab:2e4e:c61b:34ac:12f0   2019-09-07T20:29:49Z 

Load https://deno-deno-deno.fly.dev up in your browser and you should see your app!