Skip to content

Commit

Permalink
Introduce static option in Bun.serve() (#13540)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Aug 30, 2024
1 parent 59eb551 commit 1bed7a7
Show file tree
Hide file tree
Showing 7 changed files with 706 additions and 98 deletions.
22 changes: 22 additions & 0 deletions docs/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ const server = Bun.serve({
});
```

### `static` responses

Serve static responses by route with the `static` option

```ts
Bun.serve({
static: {
"/api/health-check": new Response("All good!"),
"/old-link": Response.redirect("/new-link", 301),
"/": new Response("Hello World"),
},

fetch(req) {
return new Response("404!");
},
});
```

{% note %}
`static` is experimental and may change in the future.
{% /note %}

### Changing the `port` and `hostname`

To configure which port and hostname the server will listen on, set `port` and `hostname` in the options object.
Expand Down
20 changes: 20 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,26 @@ declare module "bun" {
* This string will currently do nothing. But in the future it could be useful for logs or metrics.
*/
id?: string | null;

/**
* Server static Response objects by route.
*
* @example
* ```ts
* Bun.serve({
* static: {
* "/": new Response("Hello World"),
* "/about": new Response("About"),
* },
* fetch(req) {
* return new Response("Fallback response");
* },
* });
* ```
*
* @experimental
*/
static?: Record<`/${string}`, Response>;
}

interface ServeOptions extends GenericServeOptions {
Expand Down
Loading

0 comments on commit 1bed7a7

Please sign in to comment.