diff options
Diffstat (limited to 'docs/guides/http/hot.md')
-rw-r--r-- | docs/guides/http/hot.md | 39 |
1 files changed, 3 insertions, 36 deletions
diff --git a/docs/guides/http/hot.md b/docs/guides/http/hot.md index c033e5be5..8bc208893 100644 --- a/docs/guides/http/hot.md +++ b/docs/guides/http/hot.md @@ -10,46 +10,13 @@ bun --hot run index.ts --- -To avoid re-running `Bun.serve()` during `--hot` reloads, you should assign the `Server` instance as a property of `globalThis`. The `globalThis` object survives hot reloads. +Bun detects when you are running an HTTP server with `Bun.serve()`. It reloads your fetch handler when source files change, _without_ restarting the `bun` process. This makes hot reloads nearly instantaneous. ```ts -import { type Serve, type Server } from "bun"; - -// make TypeScript happy -declare global { - var server: Server; -} - -// define server parameters -const serveOptions: Serve = { - port: 3000, - fetch(req) { - return new Response(`Hello world`); - }, -}; - -if (!globalThis.server) { - globalThis.server = Bun.serve(serveOptions); -} else { - globalThis.server.reload(serveOptions); -} -``` - ---- - -To avoid manually calling `server.reload()`, you can use start a server with Bun's [object syntax](/docs/runtime/hot#http-servers). If you `export default` a plain object with a `fetch` handler defined, then run this file with Bun, Bun will start an HTTP server as if you'd passed this object into `Bun.serve()`. - -With this approach, Bun automatically reloads the server when reloads happen. - -See [HTTP > Hot Reloading](<[/docs/api/http](https://bun.sh/docs/api/http#hot-reloading)>) for full docs. - -```ts -import { type Serve } from "bun"; - -export default { +Bun.serve({ port: 3000, fetch(req) { return new Response(`Hello world`); }, -} satisfies Serve; +}); ``` |