> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 伺服器 Mastra 會透過 `src/mastra/` 直屬的 `server.ts` 檔案設定其 HTTP [伺服器](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)。伺服器會將 Agent、Workflow 及其他已註冊的基礎功能公開為 REST 端點,而該檔案預設匯出的 `ServerConfig` 結構,與你傳給 [`new Mastra()`](https://mastra.zisheng.pro/zh-HK/reference/core/mastra-class) 的結構相同。 本頁說明檔案式慣例。如需了解伺服器功能、中介軟件、自訂路由、自動產生的 API 文件及部署行為,請參閱[伺服器概覽](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)。 ## 快速開始 建立 `src/mastra/server.ts`,並預設匯出一個 `ServerConfig` 物件: ```typescript import type { ServerConfig } from '@mastra/core/server' export default { port: 4111, host: '0.0.0.0', } satisfies ServerConfig ``` ## 常用設定 `server.ts` 可以設定與程式碼註冊相同的伺服器選項: | 需要 | 使用 | | ------------------ | --------------------------------------- | | 綁定至部署平台的地址 | `host` 及 `port` | | 限制瀏覽器存取 | `cors` | | 加入 webhook 或健康狀態檢查 | `apiRoutes` | | 啟用自動產生的 API 文件 | `build.openAPIDocs` 及 `build.swaggerUI` | 相關範例請參閱 [Mastra 伺服器](https://mastra.zisheng.pro/zh-HK/docs/server/mastra-server)、[中介軟件](https://mastra.zisheng.pro/zh-HK/docs/server/middleware)及[自訂 API 路由](https://mastra.zisheng.pro/zh-HK/docs/server/custom-api-routes)。 ## 與程式碼設定的優先次序 以程式碼註冊的伺服器設定優先於 `server.ts`。套用檔案式伺服器設定時,系統會保留啟動期間加入的路由,例如頻道 webhook。