HTTP server: Hot reload

Restarting a server by hand after every edit gets old fast. The --watch-hmr flag hot-replaces changed modules inside the running process, so the server keeps serving while you edit.

Edit on GitHub

Nothing in the code needs to change; this is a plain HTTP server.

function handler(_req: Request): Response {
  return new Response("Hello, World!");
}

Ende.serve(handler);

Run it with the --watch-hmr flag: ende run --watch-hmr -N server.ts HMR Process started. Listening on http://0.0.0.0:8000/ Edit the response text and save. The module is swapped in place, without a restart, and the next request already returns the new text: HMR Replaced changed module file:///.../server.ts When a change cannot be hot-replaced, the process restarts automatically, so you never serve stale code.

The plain --watch flag is the simpler variant: it always restarts the whole process on every change. Use it when you want a clean slate, for example to rerun module-level setup.

By default every local file the server imports is watched. To watch extra paths, like templates or static assets, list them explicitly: ende run --watch-hmr=static/ -N server.ts

The --watch flag also works with ende serve, ende test, and most other subcommands.

Run this example locally using the Ende CLI:

ende run -N --watch-hmr https://docs.ende.sh/examples/scripts/http_server_hot_reload.ts

Additional resources