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;
```
n> Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/packages/telemetry (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-08-03Fix failed server restart calling integration hooks twice (#7917)Gravatar Bjorn Lu 5-30/+27
2023-08-02[ci] formatGravatar matthewp 3-19/+28
2023-08-02Persistent DOM in ViewTransitions (#7861)Gravatar Matthew Phillips 18-20/+242
2023-08-02[ci] formatGravatar matthewp 1-1/+1
2023-08-02Fix scroll position in view transition (#7882) (#7911)Gravatar Martin Trapp 2-1/+6
2023-08-02Improve sourcemap generation and performance (#7901)Gravatar Bjorn Lu 11-27/+34
2023-08-01[ci] formatGravatar natemoo-re 1-1/+0
2023-08-01Fix astro/app import (#7821)Gravatar ottomated 4-1/+25
2023-08-01[ci] formatGravatar natemoo-re 1-1/+1
2023-08-01feat: add cache headers to assets in Vercel adapter (#7729)Gravatar Hee 10-3/+144
2023-08-01Fix "res.writeHead is not a function" in Express/node middleware (#7708)Gravatar DixCouleur 3-8/+34
2023-08-01Fix: Content Collections - Ignore `.json` files nested in non-underscored sub...Gravatar Tony Dang 3-1/+6
2023-08-01chore: address feedback from #7754 (#7906)Gravatar Nate Moore 1-2/+4
2023-08-01[ci] release (#7877)astro@2.9.7@astrojs/vercel@3.7.5@astrojs/node@5.3.1@astrojs/netlify@2.5.2Gravatar Houston (Bot) 52-129/+128
2023-08-01feat: add logging for when hydrate's JSON parse fails (#7887)Gravatar Sam Hulick 2-3/+28
2023-08-01Fix prefetch test fail (#7902)Gravatar Bjorn Lu 1-0/+2
2023-08-01[ci] formatGravatar bluwy 4-13/+28