為什麼需要後端?
MST SDK 在瀏覽器執行測速,但需要後端提供測速節點 URL。
我們的 API 會向 CDN 服務請求高頻寬節點,然後回傳給前端使用。
這是一個單檔設計,方便部署到任何 Node.js 環境或 Serverless 平台。
API 端點
| Method | Path | 說明 |
|---|---|---|
GET | /api/mst/targets | 取得 CDN 測速節點 |
POST | /api/mst/results | 儲存測速結果 |
GET | /api/mst/health | 健康檢查 |
Response 範例
GET /api/mst/targets
{
"success": true,
"targets": [
{
"url": "https://...",
"city": "Taipei",
"country": "TW"
}
]
} 部署方式
Vercel (Serverless)
步驟
# 1. 建立專案結構
/api
└── mst/[[...path]].js # 改名後放這 # 2. 部署到 Vercel
vercel deploy
# 3. 自動變成 /api/mst/* 端點 獨立運行
Terminal
# 需要 express
npm install express
# 啟動
node mst-api.js
# 跑在 http://localhost:3001 Railway / Render
設定
# 啟動指令
node mst-api.js
# 環境變數(可選)
PORT=3001 前端整合
部署完成後,前端 SDK 指向你的 API:
JavaScript
const mst = new MST({
apiEndpoint: 'https://your-api.vercel.app/api/mst'
});
mst.on('complete', (result) => {
console.log(result.speed.value, result.speed.unit);
});
await mst.run();