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.3.2/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
GET /favicon.ico

Deploy it

Get flyctl

If you're on a Mac, you can install the CLI with Homebrew:

brew install superfly/tap/flyctl

For other systems, use the install script:

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

Log in to Fly

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 login. If you haven't yet registered with Fly, you can do that now!

Create a new Fly app

flyctl create --builder deno
App Name (leave blank to use an auto-generated name) deno-deno-deno

✔ New app created
  Name    = deno-deno-deno
  Owner   = deno
  Version = v0
  Status  =

Created 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:

[build]
  builder = "deno"
  entry_file = "./server.ts"
  deno_version = "v0.18.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
...

==> Deploying Image
--> done
Removed deployment image: registry.fly.io/deno-deno-deno:deployment-1566498016
  Version     = v0
  Reason      =
  Description =
  User        = flydev@fly.local
Build complete - succeeded

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  = 5                               
  Status   = running                         
  Hostname = https://deno-deno-deno.fly.dev  

Services
  TASK   PROTOCOL   PORT   INTERNAL PORT   HANDLERS  
  app    tcp        80     8080            http      
  app    tcp        443    8080            tls http  

IP Addresses
  ADDRESS                                TYPE  
  77.83.140.104                          v4    
  2a09:8280:1:2fab:2e4e:c61b:34ac:12f0   v6

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