运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

在 HTTP 服务器上配置 TLS

设置 tls 键以配置 TLS。key 和 cert 都是必需的:key 是私钥的内容,cert 是已签发证书的内容。使用 Bun.file() 读取它们。

server.ts
const server = Bun.serve({
  fetch: request => new Response("Welcome to Bun!"),
  tls: {
    cert: Bun.file("cert.pem"),
    key: Bun.file("key.pem"),
  },
});

默认情况下,Bun 信任由 Mozilla 筛选的知名根 CA 列表。若要覆盖此列表,请将证书数组作为 ca 传入。在服务器上,Bun 使用此列表验证 客户端 证书,因此还要设置 requestCert: true。

server.ts
const server = Bun.serve({
  fetch: request => new Response("Welcome to Bun!"),
  tls: {
    cert: Bun.file("cert.pem"),
    key: Bun.file("key.pem"),
    ca: [Bun.file("ca1.pem"), Bun.file("ca2.pem")],
    requestCert: true,
  },
});