Foundational Compute

Preview a web app

Start a web app on a public port and share its URL.

Templatebase (2 vCPU, 512 MiB, $0.039 per hour)
Run timeAbout 3 seconds to a working URL
Cost of a run$0.020 for a preview that stays up 30 minutes
Cleanupkill(), the 30-minute lifetime, or fdtn kill --all -m example=preview

1The code

Replace the small Node.js server with your own app.

Python · TypeScript

preview.py
import time, urllib.request
from fdtn import Sandbox

SERVER = """
const http = require('http')
let hits = 0
http.createServer((req, res) => {
  hits += 1
  res.setHeader('content-type', 'application/json')
  res.end(JSON.stringify({ path: req.url, hits, node: process.version }))
}).listen(3000, '0.0.0.0')
"""

sbx = Sandbox.create(
    "base",
    timeout=1800,                                # the preview lives 30 minutes
    network={"allow_public_traffic": True},      # anyone with the URL can open it
    metadata={"example": "preview"},
)
# 1. Write the app and start it in the background.
sbx.files.write("/home/user/app/server.js", SERVER)
sbx.commands.run("node server.js", cwd="/home/user/app", background=True, timeout=0)
# 2. Wait until the server answers inside the sandbox.
sbx.commands.run(
    "for i in $(seq 1 30); do curl -sf localhost:3000 >/dev/null && exit 0; sleep 1; done; exit 1",
    timeout=40,
)
# 3. Open the public URL from your computer.
url = f"https://{sbx.get_host(3000)}"
print("preview:", url)
for path in ["/", "/about"]:
    with urllib.request.urlopen(url + path, timeout=30) as r:
        print(r.status, r.read().decode())
# 4. Stop the preview when you are done. Until then, share the URL.
sbx.kill()

2Run it

The first hit is the check in step 2:

$ python preview.py      # or: node preview.mjs
preview: https://3000-i3os3vs4t3x4kbjf8pikmd9k.sbx.fdtn.dev
200 {"path":"/","hits":2,"node":"v20.9.0"}
200 {"path":"/about","hits":3,"node":"v20.9.0"}

To keep the preview up for people, remove the last line, kill(). The URL works until the 30-minute lifetime ends.

Notes

  • allow_public_traffic / allowPublicTraffic makes all ports public. Set it at create. Private and public ports
  • Listen on 0.0.0.0, not only localhost.
  • timeout=0 keeps a background server running. The 60-second default stops it.
  • HTTPS ends at our side, so the app serves plain HTTP. WebSocket also works.
  • For Vite or Next.js, set the host to 0.0.0.0 and allow the *.sbx.fdtn.dev host name.

Anyone with a public URL can send requests to it. Do not serve secrets or private data from a public port.

Next