aboutsummaryrefslogtreecommitdiff
path: root/docs/api/http.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api/http.md')
-rw-r--r--docs/api/http.md83
1 files changed, 56 insertions, 27 deletions
diff --git a/docs/api/http.md b/docs/api/http.md
index 3ebdafab9..567560c3c 100644
--- a/docs/api/http.md
+++ b/docs/api/http.md
@@ -1,19 +1,12 @@
+The page primarily documents the Bun-native `Bun.serve` API. Bun also implements [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules.
+
{% callout %}
-**Note** — This page documents the `Bun.serve` API. This API is heavily optimized and represents the recommended way to build HTTP servers in Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/runtime/nodejs-apis#node_http) implementation of the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules.
+These modules have been re-implemented to use Bun's fast internal HTTP infrastructure. Feel free to use these modules directly; frameworks like [Express](https://expressjs.com/) that depend on these modules should work out of the box. For granular compatibility information, see [Runtime > Node.js APIs](/docs/runtime/nodejs-apis).
{% /callout %}
-## Send a request (`fetch()`)
-
-Bun implements the Web `fetch` API for making HTTP requests. The `fetch` function is available in the global scope.
+To start a high-performance HTTP server with a clean API, the recommended approach is [`Bun.serve`](#start-a-server-bun-serve).
-```ts
-const response = await fetch("https://bun.sh/manifest.json");
-const result = (await response.json()) as any;
-console.log(result.icons[0].src);
-// => "/logo-square.jpg"
-```
-
-## Start a server (`Bun.serve()`)
+## `Bun.serve()`
Start an HTTP server in Bun with `Bun.serve`.
@@ -50,7 +43,7 @@ Bun.serve({
});
```
-### Error handling
+## Error handling
To activate development mode, set `development: true`. By default, development mode is _enabled_ unless `NODE_ENV` is `production`.
@@ -100,36 +93,72 @@ const server = Bun.serve({
server.stop();
```
-### TLS
+## TLS
-Bun supports TLS out of the box, powered by [OpenSSL](https://www.openssl.org/). Enable TLS by passing in a value for `keyFile` and `certFile`; both are required to enable TLS. If needed, supply a `passphrase` to decrypt the `keyFile`.
+Bun supports TLS out of the box, powered by [OpenSSL](https://www.openssl.org/). Enable TLS by passing in a value for `key` and `cert`; both are required to enable TLS. If needed, supply a `passphrase` to decrypt the `keyFile`.
```ts
Bun.serve({
fetch(req) {
return new Response("Hello!!!");
},
- keyFile: "./key.pem", // path to TLS key
- certFile: "./cert.pem", // path to TLS cert
- passphrase: "super-secret", // optional passphrase
+
+ // can be string, BunFile, TypedArray, Buffer, or array thereof
+ key: Bun.file("./key.pem"),
+ cert: Bun.file("./cert.pem"),
+
+ // passphrase, only required if key is encrypted
+ passphrase: "super-secret",
+});
+```
+
+The `key` and `cert` fields expect the _contents_ of your TLS key and certificate. This can be a string, `BunFile`, `TypedArray`, or `Buffer`.
+
+```ts
+Bun.serve({
+ fetch() {},
+
+ // BunFile
+ key: Bun.file("./key.pem"),
+ // Buffer
+ key: fs.readFileSync("./key.pem"),
+ // string
+ key: fs.readFileSync("./key.pem", "utf8"),
+ // array of above
+ key: [Bun.file('./key1.pem'), Bun.file('./key2.pem']
+
});
```
-The root CA and Diffie-Helman parameters can be configured as well.
+{% callout %}
+
+**Note** — Earlier versions of Bun supported passing a file path as `keyFile` and `certFile`; this has been deprecated as of `v0.6.3`.
+
+{% /callout %}
+
+Optionally, you can override the trusted CA certificates by passing a value for `ca`. By default, the server will trust the list of well-known CAs curated by Mozilla. When `ca` is specified, the Mozilla list is overwritten.
```ts
Bun.serve({
fetch(req) {
return new Response("Hello!!!");
},
- keyFile: "./key.pem", // path to TLS key
- certFile: "./cert.pem", // path to TLS cert
- caFile: "./ca.pem", // path to root CA certificate
- dhParamsFile: "./dhparams.pem", // Diffie Helman parameters
+ key: Bun.file("./key.pem"), // path to TLS key
+ cert: Bun.file("./cert.pem"), // path to TLS cert
+ ca: Bun.file("./ca.pem"), // path to root CA certificate
+});
+```
+
+To override Diffie-Helman parameters:
+
+```ts
+Bun.serve({
+ // ...
+ dhParamsFile: "./dhparams.pem", // path to Diffie Helman parameters
});
```
-### Hot reloading
+## Hot reloading
Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax.
@@ -153,7 +182,7 @@ $ bun --hot server.ts
It's possible to configure hot reloading while using the explicit `Bun.serve` API; for details refer to [Runtime > Hot reloading](/docs/runtime/hot).
-### Streaming files
+## Streaming files
To stream a file, return a `Response` object with a `BunFile` object as the body.
@@ -191,7 +220,7 @@ Bun.serve({
});
```
-### Benchmarks
+## Benchmarks
Below are Bun and Node.js implementations of a simple HTTP server that responds `Bun!` to each incoming `Request`.
@@ -234,7 +263,7 @@ The `Bun.serve` server can handle roughly 2.5x more requests per second than Nod
{% image width="499" alt="image" src="https://user-images.githubusercontent.com/709451/162389032-fc302444-9d03-46be-ba87-c12bd8ce89a0.png" /%}
-### Reference
+## Reference
{% details summary="See TypeScript definitions" %}