快速開始
使用 Bun 建置您的第一個應用
概覽
使用 Bun.serve 建置一個簡約的 HTTP 伺服器,在本地執行它,然後通過安裝一個包來擴充套件它。
- 步驟 1
使用
bun init初始化一個新專案。terminalbun init my-appbun init會提示您選擇一個模板:Blank、React或Library。對於本指南,請選擇Blank。terminalbun init my-appText✓ 選擇一個專案模板:Blank + .gitignore + CLAUDE.md + .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc -> CLAUDE.md + index.ts + tsconfig.json (for editor autocomplete) + README.md新的
my-app目錄包含一個基本的 Bun 應用。 - 步驟 2
使用
bun run執行index.ts。terminalcd my-app bun run index.tsTextHello via Bun! - 步驟 3
將
index.ts的內容替換為以下程式碼:index.ts
const server = Bun.serve({ port: 3000, routes: { "/": () => new Response('Bun!'), } }); console.log(`Listening on ${server.url}`);再次執行
index.ts。terminalbun run index.tsTextListening on http://localhost:3000/訪問
http://localhost:3000測試伺服器。您應該看到一個顯示"Bun!"的頁面。在 Bun 中看到 TypeScript 錯誤?
bun init會安裝 Bun 的 TypeScript 宣告並設定您的tsconfig.json。如果您正在現有專案中嘗試使用 Bun,可能會在Bun全域性變數上看到型別錯誤。要解決此問題,首先安裝
@types/bun作為開發相依。terminalbun add -d @types/bun然後在
tsconfig.json的compilerOptions中新增以下設定:tsconfig.json{ "compilerOptions": { "lib": ["ESNext"], "target": "ESNext", "module": "Preserve", "moduleDetection": "force", "moduleResolution": "bundler", "allowImportingTsExtensions": true, "verbatimModuleSyntax": true, "noEmit": true } } - 步驟 4
安裝
figlet包及其型別宣告。Figlet 是一個將字串轉換為 ASCII 藝術的工具。terminalbun add figlet bun add -d @types/figlet # 僅限 TypeScript 使用者更新
index.ts,在routes中使用figlet。index.ts
import figlet from 'figlet'; // [!code ++] const server = Bun.serve({ port: 3000, routes: { "/": () => new Response('Bun!'), "/figlet": () => { // [!code ++] const body = figlet.textSync('Bun!'); // [!code ++] return new Response(body); // [!code ++] } // [!code ++] } }); console.log(`Listening on ${server.url}`);再次執行
index.ts。terminalbun run index.tsTextListening on http://localhost:3000/訪問
http://localhost:3000/figlet測試伺服器。您應該看到以 ASCII 藝術形式顯示的"Bun!"。Text____ _ | __ ) _ _ _ __ | | | _ \| | | | '_ \| | | |_) | |_| | | | |_| |____/ \__,_|_| |_(_) - 步驟 5
現在新增一些 HTML。建立一個名為
index.html的新檔案:index.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bun</title> </head> <body> <h1>Bun!</h1> </body> </html>然後,在
index.ts中匯入此檔案,並從根路由/提供它。index.ts
import figlet from 'figlet'; import index from './index.html'; // [!code ++] const server = Bun.serve({ port: 3000, routes: { "/": index, // [!code ++] "/figlet": () => { const body = figlet.textSync('Bun!'); return new Response(body); } } }); console.log(`Listening on ${server.url}`);再次執行
index.ts。terminalbun run index.tsTextListening on http://localhost:3000/訪問
http://localhost:3000測試伺服器。您應該看到該靜態 HTML 頁面。
您已經使用 Bun 建置了一個 HTTP 伺服器並安裝了一個包。
執行指令碼
Bun 也可以執行 package.json 中的 "scripts"。新增以下指令碼:
{
"name": "my-app",
"module": "index.ts",
"type": "module",
"private": true,
"scripts": { // [!code ++]
"start": "bun run index.ts" // [!code ++]
}, // [!code ++]
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^7"
}
}
然後通過 bun run start 執行它。
bun run start
Listening on http://localhost:3000/