← Foundations Course

SYSTEM FOUNDATIONS · 10

Client / Server / API:同一個功能其實跨了多個 Runtime

Client 是發出請求、呈現互動的一端;Server 是接收 request、執行可信邏輯與存取資料的一端;API 是兩者約定的 interface。這三者不要混成「網站」。

Learning outcomes

1. Client

Browser
  HTML / CSS / JS
  local UI state
  user events
  network requests

Client code 在使用者環境執行,使用者可以觀察甚至修改,因此不能當可信 security boundary。

2. Server

Server process
  routes
  validation
  authentication
  authorization
  business logic
  database access

Server 是可信邊界的一部分,但它本身也要正確實作 policy/validation。

3. API 是 Contract

GET /api/courses

Response 200:
[
  {
    "id": 1,
    "title": "JavaScript"
  }
]

Contract 包含 method/path、input、status、response shape、auth requirement,不只是一個 URL。

4. Browser fetch

const response =
  await fetch(
    "/api/courses"
  );

if (!response.ok) {
  throw new Error(
    "HTTP "
    + response.status
  );
}

const courses =
  await response.json();

fetch resolve 不代表 HTTP 2xx;要檢查 response status。

5. Trust Boundary

Browser says:
score = 999
owner_id = "admin"

Server must:
validate score
derive trusted identity
authorize operation

Client validation 是 UX;Server validation/authorization 才能保護資源。

Project checkpoint:Request Journey Map v10

User clicks "Load courses"
  ↓
Client event handler
  ↓
GET /api/courses
  ↓ DNS/TLS/HTTP
Server route
  ↓
business/data layer
  ↓
JSON response
  ↓
Client state
  ↓
DOM render

至此 01–10 已經把「輸入網址到互動畫面」串成一條完整骨架;後面再深入 DB/Auth/Git/Cloud。

Debug evidence:UI 顯示錯誤要往哪查

  1. event 有沒有觸發?
  2. request 有沒有送出?
  3. status/body 是什麼?
  4. server log 是否收到 request?
  5. response 進 state 了嗎?
  6. render 是否使用正確 state?

Knowledge check

  1. API 為什麼不只是 endpoint URL?
  2. Client 為什麼不可信?
  3. fetch() 成功 resolve 是否等於 HTTP 200?
  4. 畫出一次 Course API request 的 client/server runtime boundary。