On this page
HTTP serving
Available in Deno 2.9
deno desktop is available starting in Deno v2.9.0. If you're on an earlier
version, update Deno to use it.
A deno desktop app serves its UI over local HTTP and points the embedded
webview at it. This keeps the app structure identical to a normal Deno website.
Deno.serve() is the entry point and every request
flows through your handler, but with no port to manage and no remote network
exposure.
How it works Jump to heading
When the binary starts:
- The runtime picks an unused local port and sets the
DENO_SERVE_ADDRESSenvironment variable totcp:127.0.0.1:<port>. - Your code calls
Deno.serve(...). The serve API readsDENO_SERVE_ADDRESS(set by Deno itself in this mode, not by the user) and binds to that port, ignoring whatever port you pass. - The webview navigates to
http://127.0.0.1:<port>once the listener is ready.
You write the same handler you would for any Deno HTTP server. There is no desktop-specific serving API.
main.ts
Deno.serve((req) => {
const url = new URL(req.url);
if (url.pathname === "/api/hello") {
return Response.json({ hello: "world" });
}
return new Response(HOMEPAGE, {
headers: { "content-type": "text/html" },
});
});
const HOMEPAGE = `<!doctype html>
<html><body>
<h1>Hello, desktop</h1>
<button onclick="fetch('/api/hello').then(r => r.json()).then(console.log)">
Ping
</button>
</body></html>`;