Bun

指南生態系統

使用 StricJS 和 Bun 建立 HTTP 伺服器

StricJS 是一個 Bun 框架,用於構建高效能的網路應用程式和 API。

  • 快速 — Stric 是最快的 Bun 框架之一。請參閱 benchmark 以了解更多詳細資訊。
  • 極簡 — 像是 @stricjs/router@stricjs/utils 等基本組件小於 50kB,且不需要外部依賴。
  • 可擴展 — Stric 包含外掛系統、依賴注入和處理請求的可選優化。

使用 bun init 建立一個空專案。

mkdir myapp
cd myapp
bun init
bun add @stricjs/router @stricjs/utils

要使用 StricJS 實作一個簡單的 HTTP 伺服器

index.ts
import { Router } from '@stricjs/router';

export default new Router()
  .get('/', () => new Response('Hi'));

/public 提供靜態檔案

index.ts
import { dir } from '@stricjs/utils';

export default new Router()
  .get('/', () => new Response('Hi'))
  .get('/*', dir('./public'));

在監看模式下執行檔案以啟動開發伺服器。

bun --watch run index.ts

如需更多資訊,請參閱 Stric 的文件