HTTP serving

A ende desktop app serves its UI with Ende.serve() and the embedded webview loads it. This keeps the app structure identical to a normal Ende website: every request flows through your handler, with no port to manage and no remote network exposure.

Ende 2026.11.10

The webview backend on macOS and Windows loads the app from app://<host> over an in-process transport, so the page keeps its storage across launches. Earlier releases served every backend on a random loopback port.

How it works

When the binary starts:

  1. Before any of your code runs, the runtime picks the address the app serves on and sets the ENDE_SERVE_ADDRESS environment variable to it.
  2. Your code calls Ende.serve(...). The first Ende.serve() call binds to that address, ignoring the hostname and port you pass.
  3. The webview loads the app once your server answers GET / with a 2xx or 3xx response. If it doesn't within 15 seconds, Ende logs "Server not ready after 15s, navigating anyway" and loads the app anyway.

Where the page is served from depends on the backend and the operating system:

Backend Page origin ENDE_SERVE_ADDRESS
webview on macOS, Windows app://<host> memory:<host>
webview on Linux, cef http://127.0.0.1:<port> tcp:127.0.0.1:<port>
raw none (no web page) tcp:127.0.0.1:<port>

<host> is the app identifier, lowercased (see app.identifier). <port> is a random free port, picked again on every launch.

You write the same handler you would for any Ende HTTP server. There is no desktop-specific serving API.

main.ts
Ende.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>`;
ende desktop main.ts

The default-export form works too:

main.ts
export default {
  fetch(req: Request): Response {
    return new Response("Hello!");
  }
};

Relative URLs in the page (fetch("/api/hello"), <a href="/settings">) work the same on every backend.

The app:// origin

On the webview backend on macOS and Windows, the page runs at app://<host>. The app serves on an in-process listener named <host>, and the webview's requests are forwarded to it. No TCP port serves the page. Because the origin is the same on every launch, the page's localStorage and IndexedDB persist between launches. Changing the app identifier, or renaming an app that has no desktop.app.identifier set, gives the page a new origin with empty storage.

What your handler sees on this transport:

  • request.url is http://<host>/….
  • info.remoteAddr.transport is "memory". Use it to tell requests from the app's own window apart from requests to any TCP server the app runs.
  • A Location, Content-Location or Refresh header pointing at http://<host> is rewritten to app://<host> on the way out, so Response.redirect(new URL("/next", req.url)) works.
  • Request bodies are limited to 4 MiB. A larger body gets a 413 response.

Platform differences:

  • macOS: a Blob request body arrives empty. Strings and FormData arrive.
  • macOS: WKWebView doesn't follow a redirect from this origin, so Ende follows same-origin redirects itself. A navigation ends up at the target page (a form POST answered with 303 lands on a GET). For fetch, XHR and images, Ende follows the redirect with the Fetch rules, up to 20 hops, but response.redirected is false, response.url is the first URL, and redirect: "manual" is followed too. A redirect to another origin isn't followed.
  • macOS: Content-Security-Policy: frame-ancestors and X-Frame-Options don't stop another page from framing an app:// page. A page that mustn't be framed has to check window.top === window itself.
  • Windows: the WebView2 host hands the page a response only once the whole of it has arrived, so server-sent events and other streamed responses would arrive only when they end. This comes from how the host is written and is untested. Redirects are passed to WebView2 unchanged; whether it follows them is untested too.

WebSockets

The webview can't open a ws:// connection to app://<host>. Instead, a relay on a random loopback port forwards WebSocket upgrades to your server. Its URL is in the ENDE_DESKTOP_WS_URL environment variable, which only the app's runtime sees (child processes don't inherit it). The relay accepts only connections from the app's origin that carry this launch's token.

Hand the URL to the page, and fall back to the page's own host on backends that serve over loopback, where ENDE_DESKTOP_WS_URL isn't set:

main.ts
Ende.serve((req) => {
  const url = new URL(req.url);
  if (url.pathname === "/config") {
    return Response.json({ ws: Ende.env.get("ENDE_DESKTOP_WS_URL") ?? null });
  }
  if (url.pathname === "/ws") {
    const { socket, response } = Ende.upgradeWebSocket(req);
    socket.onmessage = (e) => socket.send(e.data);
    return response;
  }
  return new Response(HOMEPAGE, { headers: { "content-type": "text/html" } });
});
In the page
const { ws } = await (await fetch("/config")).json();
const socket = new WebSocket(ws ? ws + "/ws" : `ws://${location.host}/ws`);

Append your path to the relay URL: <relay URL>/ws?x=1 reaches your server as GET /ws?x=1. Requests that came through the relay carry an x-ende-desktop-relay: 1 header.

Cookies

On macOS, WKWebView keeps no cookies for app://, so Ende keeps a cookie jar for the app. It stores the Set-Cookie headers of your responses and sends matching cookies with later requests and WebSocket upgrades. A login cookie works as in a browser, with these differences:

  • document.cookie can't read or set these cookies.
  • Session cookies last until the app quits. Persistent cookies are saved in ~/Library/Application Support/<identifier>/cookies.json, so a login survives a relaunch.
  • An XHR POST is sent without cookies, and the Set-Cookie of an XHR response is dropped. Use fetch() or a form for logins and other state changes.
  • SameSite=Strict cookies aren't sent with XHR, images, scripts, stylesheets, the first load or navigate(). Use Lax (or no SameSite) for a session cookie.
  • Ende can't tell a GET without an Origin or Referer header (XHR, images, scripts) from the app apart from one made by a remote page the window navigated to, and sends Lax cookies with both. Keep state changes off GET, and send X-Content-Type-Options: nosniff with JSON responses.

On Windows, cookie handling for app:// pages is left to WebView2 and is untested.

Loopback backends

The CEF backend, the webview backend on Linux and the raw backend serve on http://127.0.0.1:<port>. The port changes on every launch, and with it the page's origin, so the page's localStorage and IndexedDB start empty on every launch. Store state that must persist in the app's runtime instead, for example with localStorage in your Ende code or in files.

The bound address is always 127.0.0.1, even if you pass 0.0.0.0 to Ende.serve(), so other machines can't reach it.

If you need to serve users on other machines (a self-hosted local server), do not use ende desktop for that part of your stack. Use ende run with an explicit address, or build a separate service.

Building URLs to your own pages

APIs like BrowserWindow.navigate() take an absolute URL. In the page, build it from location.origin. In your Ende code, build it from ENDE_SERVE_ADDRESS:

function appOrigin(): string {
  const addr = Ende.env.get("ENDE_SERVE_ADDRESS")!;
  if (addr.startsWith("memory:")) {
    return `app://${addr.slice("memory:".length)}`; // "memory:com.example.myapp"
  }
  return `http://127.0.0.1:${addr.split(":").pop()}`; // "tcp:127.0.0.1:54321"
}

const settings = new Ende.BrowserWindow();
settings.navigate(new URL("/settings", appOrigin()).href);

Reading ENDE_SERVE_ADDRESS needs --allow-env=ENDE_SERVE_ADDRESS (or a wider --allow-env) when you build. ENDE_DESKTOP_WS_URL can be read without it.

ENDE_SERVE_ADDRESS and ENDE_DESKTOP_WS_URL are set for the app's runtime only: child processes the app starts don't inherit them. You can't set a memory: address yourself; Ende refuses ENDE_SERVE_ADDRESS=memory:… from the environment.

Serving multiple windows

All windows load from the same server. Use different paths per window to differentiate them, as in the navigate() example above.

For high-throughput Ende → webview communication, use bindings, which bypass HTTP entirely and route through in-process channels.