> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Server Mastra 會透過 `src/mastra/` 正下方的 `server.ts` 檔案設定 HTTP [server](https://mastra.zisheng.pro/zh-TW/docs/server/mastra-server)。此 server 會將 Agent、Workflow 與其他已註冊的 primitive 公開為 REST endpoint,而該檔案會 default export 與傳給 [`new Mastra()`](https://mastra.zisheng.pro/zh-TW/reference/core/mastra-class) 相同的 `ServerConfig` 結構。 本頁說明檔案式慣例。若要了解 server 功能、middleware、自訂 route、產生的 API 文件與部署行為,請參閱 [Server 概觀](https://mastra.zisheng.pro/zh-TW/docs/server/mastra-server)。 ## 快速開始 建立 `src/mastra/server.ts`,並 default export 一個 `ServerConfig` 物件: ```typescript import type { ServerConfig } from '@mastra/core/server' export default { port: 4111, host: '0.0.0.0', } satisfies ServerConfig ``` ## 常見設定 `server.ts` 可以設定與程式碼註冊相同的 server 選項: | 需求 | 使用方式 | | ------------------ | --------------------------------------- | | 繫結至部署平台位址 | `host` 和 `port` | | 限制瀏覽器存取 | `cors` | | 新增 webhook 或健康狀態檢查 | `apiRoutes` | | 啟用產生的 API 文件 | `build.openAPIDocs` 和 `build.swaggerUI` | 相關範例請參閱 [Mastra server](https://mastra.zisheng.pro/zh-TW/docs/server/mastra-server)、[middleware](https://mastra.zisheng.pro/zh-TW/docs/server/middleware)和[自訂 API route](https://mastra.zisheng.pro/zh-TW/docs/server/custom-api-routes)。 ## 與程式碼設定的優先順序 以程式碼註冊的 server 設定優先於 `server.ts`。套用檔案式 server 設定時,啟動期間新增的 route(例如 channel webhook)會予以保留。