Preview a web app
Start a web app on a public port and share its URL.
| Template | base (2 vCPU, 512 MiB, $0.039 per hour) |
|---|---|
| Run time | About 3 seconds to a working URL |
| Cost of a run | $0.020 for a preview that stays up 30 minutes |
| Cleanup | kill(), the 30-minute lifetime, or fdtn kill --all -m example=preview |
1The code
Replace the small Node.js server with your own app.
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()
import { Sandbox } from '@fdtn/sdk' const 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') ` const sbx = await Sandbox.create('base', { timeoutMs: 1_800_000, // the preview lives 30 minutes network: { allowPublicTraffic: true }, // anyone with the URL can open it metadata: { example: 'preview' }, }) // 1. Write the app and start it in the background. await sbx.files.write('/home/user/app/server.js', SERVER) await sbx.commands.run('node server.js', { cwd: '/home/user/app', background: true, timeoutMs: 0 }) // 2. Wait until the server answers inside the sandbox. await sbx.commands.run( 'for i in $(seq 1 30); do curl -sf localhost:3000 >/dev/null && exit 0; sleep 1; done; exit 1', { timeoutMs: 40_000 }, ) // 3. Open the public URL from your computer. const url = `https://${sbx.getHost(3000)}` console.log('preview:', url) for (const path of ['/', '/about']) { const r = await fetch(url + path) console.log(r.status, await r.text()) } // 4. Stop the preview when you are done. Until then, share the URL. await 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/allowPublicTrafficmakes all ports public. Set it at create. Private and public ports- Listen on
0.0.0.0, not onlylocalhost. timeout=0keeps 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.0and allow the*.sbx.fdtn.devhost name.
Anyone with a public URL can send requests to it. Do not serve secrets or private data from a public port.