aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/http/hot.md
blob: c033e5be5c7b90082fd68b6a2417de20105a01b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
name: Hot reload an HTTP server
---

Bun supports the [`--hot`](/docs/runtime/hot#hot-mode) flag to run a file with hot reloading enabled. When any module or file changes, Bun re-runs the file.

```sh
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.

```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 {
  port: 3000,
  fetch(req) {
    return new Response(`Hello world`);
  },
} satisfies Serve;
```