框架感知路由

URL 模式 → handler——跨 17 個 framework 的 conventions 都進圖譜
route 節點 · references 邊 · 查 view/controller 的 caller 會帶出 URL 模式

在做什麼

CodeGraph 偵測 web framework 的路由檔案,產出 route 節點,用 references 邊連結到對應的 handler class / 函式。你查某個 view / controller 的 callers 時,會順帶看到綁定它的 URL 模式——「這支 API 誰在呼叫?」變成「這支 API 對應哪條 URL、被誰呼叫」一次回答。

17 個框架一覽

Framework認得的寫法
Djangopath()、re_path()、url()、include() in urls.py(CBV .as_view()、dotted paths)
Flask@app.route('/path', methods=[...])、blueprint routes
FastAPI@app.get(...)、@router.post(...)、所有標準 methods
Expressapp.get(...)、router.post(...) 帶 middleware chain
NestJS@Controller + @Get/@Post/…、GraphQL @Resolver + @Query/@Mutation、@MessagePattern/@EventPattern、@SubscribeMessage
LaravelRoute::get()、Route::resource()、Controller@action、tuple syntax
Drupal*.routing.yml routes(_controller、_form、entity handlers);hook_* 在 .module/.theme/.install/.inc
Railsget '/x', to: 'users#index'、hash-rocket => syntax
Spring@GetMapping、@PostMapping、@RequestMapping on methods
PlayGET/POST/… verb routes in conf/routes → Controller.method actions(Scala + Java)
Gin / chi / gorilla / muxr.GET(...)、router.HandleFunc(...)
Axum / actix / Rocket.route("/x", get(handler))
ASP.NET[HttpGet("/x")] attributes on action methods
Vaporapp.get("x", use: handler)
React Router / SvelteKitRoute component nodes
Vue Router / Nuxtpages/ file-based routes、server/api/ endpoints、route middleware
Astrosrc/pages/ file-based routes(.astro pages + .ts endpoints、[param]/[...rest] syntax)

實測驗證

官方在每個框架的 canonical app 上驗證過路由解析覆蓋率:Express 100%、FastAPI 98%、Flask 100%、NestJS 96.8%、Gin 96.5%、Axum 100%、Rocket 93.8%、Vapor 100%、Laravel 92%、Rails 89.6%、React Router 100%;convention/reflection 較重的則誠實停在靜態分析天花板:ASP.NET 83.9%、Spring 83.3%、Drupal 78.9%、Play 76.3%、Django 74.1%。SvelteKit / Vue / Nuxt / Astro 用 file-based routing,頁面/端點覆蓋率就是該語言本身的分數。

本站實測

codegraph 自己的 repo 就有 16 個 route 節點(codegraph status 的 Nodes by Kind 顯示 route 16)——它在自己身上也偵測到了框架路由。

看完這頁你應該能說出:route 節點如何產生(references 邊)、兩個你熟悉框架的認得寫法、以及為什麼 convention-heavy 框架(Spring/ASP.NET/Django)的覆蓋率較低是「誠實的靜態分析天花板」。

🎓 教學解說

框架感知路由是 CodeGraph 的「超能力」——它能自動連結「URL 端點」與「處理函式」,這是純靜態分析做不到的。

支援的框架分為兩類:

Worked Example:追蹤 API 請求的完整路徑

路由解析實測
# 步驟 1:搜尋 route 節點
codegraph query /api/users --kind route
# Search Results for "/api/users":
#   route       /api/users
#     src/routes/users.ts:15

# 步驟 2:查看 handler 的 callers
codegraph callers usersController.list
# Callers of "usersController.list" (2):
#   route       /api/users
#     src/routes/users.ts:15
#   file        users.test.ts
#     __tests__/users.test.ts:1

# 步驟 3:分析影響半徑
codegraph impact usersController.list --depth 2
# Impact of changing "usersController.list" — 8 affected symbols:
#   src/routes/users.ts
#     route       /api/users:15
#   src/services/userService.ts
#     method      UserService.findAll:45
#   ...

關鍵觀察:從 URL 到 handler 到 service 層,整個請求處理鏈一次可見。這在大型專案中特別有價值。

常見錯誤與診斷

症狀原因解法
route 節點未出現框架未被支援或路由定義方式特殊確認框架在支援清單中,檢查路由寫法是否符合慣例
route 連結到錯誤的 handler名稱衝突或慣例推斷錯誤用 codegraph node 手動驗證連結
覆蓋率低框架使用大量 reflection/DI這是靜態分析的限制,考慮用 include 補充

練習:驗收清單


🔍 進階真實情境 Worked Example:Rails → FastAPI 遷移的 Route Parity 稽核

前面的範例是在單一框架內追蹤請求。這裡是遷移場景:把一個 Rails app 搬到 FastAPI,上線前要確認「每條舊 URL 都有新對應」,且 handler 對齊。這是 route 節點最被低估的用法。

route parity 工作流
# ① 在舊 repo 抓全部 route
git clone --depth 1 repo-old && cd repo-old && codegraph init
codegraph query "" --kind route --json | jq -r '.results[].name' > /tmp/old-routes.txt

# ② 在新 repo 抓全部 route
cd ../repo-new && codegraph init
codegraph query "" --kind route --json | jq -r '.results[].name' > /tmp/new-routes.txt

# ③ 差集 = 還沒搬或搬漏的 URL
comm -23 /tmp/old-routes.txt /tmp/new-routes.txt   # 舊有新的沒有
comm -13 /tmp/old-routes.txt /tmp/new-routes.txt   # 新的多出來(可能是有意的重構)

# ④ 對每一條「斷裂」URL,從 handler 反查 source
codegraph callers /api/users   # 看 Rails 側 handler(route → controller#action)
codegraph callers /api/users   # 看 FastAPI 側 handler(route → function)

為什麼走這條路徑:路由是公開 API 的契約。用 route 節點把兩邊的「URL 集合」機械式比對,比人眼掃 routes 檔可靠——尤其 URL 數上百條時。剩下來的才是真正需要人工判斷的語意差異(參數改名、REST 動詞調整)。

🔬 深入原理擴充

內部架構:detect() 的時機是設計細節。framework resolver 的 detect() 在「檔案存在前」就跑一次(createResolver 建在 index 之前),所以 indexAll 在 populate 後必須 resolver.initialize() + runPostExtract() 重跑偵測——否則依檔案清單的 detect 會全 return false 而靜默棄權。跨檔 finalization(NestJS RouterModule prefixes)也在此時完成。這解釋了為什麼「同一個 repo 重跑 index」結果可能不同:第一次可能少了依賴 populate 順序的 route。

大家以為分析正確但其實有誤:以為「route 覆蓋率 = 框架支援品質」。實際覆蓋率是在 canonical app 上實測的:Explicit 框架(Express 100%、FastAPI 98%)高,convention/reflection 較重的(Spring 83.3%、Django 74.1%)誠實停在「靜態分析天花板」——框架用 DI container 或動態 URL 時,route 節點就是抓不到。另一個誤解:以為 route 節點一定連到「正確」的 handler,名稱衝突或慣例誤判時它會連錯,需要人工驗證。

🩺 診斷式疑難排解表

症狀可能原因解決方案
找不到某條已知存在的路由寫法不在支援範圍,或 detect() 未重跑確認框架支援清單與寫法慣例;檢查 runPostExtract 有執行
route 連到錯的 handler名稱衝突或慣例誤判codegraph node 手動驗證,必要時改寫法或補 resolver
相同路由重複出現多次include()/blueprint 被展開多次檢查是否有重複 import 或 prefix 疊加
覆蓋率偏低且無法改善框架用 reflection/DI接受靜態分析天花板;用 include 補已知入口
動態路由(URL 由 DB/config 產生)沒進圖非靜態字面,無法解析這屬解析極限,需手動補節點或文件說明

🧩 進階挑戰題

  1. 遷移 parity 檢查中,URL 參數語法不同(Rails /users/:id vs FastAPI /users/{id})會讓 comm 誤判為斷裂。設計一個正規化規則,並說明哪種差異該忽略、哪種該人工審。
  2. Spring 用 annotation、Express 用 method chain——為什麼前者覆蓋率較低?「靜態分析天花板」的具體來源(DI、annotation 組合、reflection)各在哪裡斷掉?
  3. Laravel 的 handler 是 dotted string(Controller@action)。resolver 要如何驗證該字串真的指向存在的 method?如果指向不存在的 method,你希望圖譜怎麼表示——斷掉的邊、警告、還是直接不建邊?

🏗️ 專案級端到端 Worked Example:跨框架 API 合約審計

一個同時用 Express(Node.js)與 FastAPI(Python)的微服務架構,要確認「每個 API 端點都有對應的 route 節點」且「handler 邏輯正確連結」:

跨框架 API 審計
# ① 分別索引兩個服務
cd api-gateway && codegraph init       # Express
cd ../ml-service && codegraph init     # FastAPI

# ② 抓出全部 route 節點
cd api-gateway
codegraph query "" --kind route --json | jq -r '.results[].name' | sort > /tmp/express-routes.txt

cd ../ml-service
codegraph query "" --kind route --json | jq -r '.results[].name' | sort > /tmp/fastapi-routes.txt

# ③ 比對:找出「有 Express route 但沒有 FastAPI 對應」的端點
comm -23 /tmp/express-routes.txt /tmp/fastapi-routes.txt

# ④ 對每一條「斷裂」URL,從 handler 反查 source
cd api-gateway
codegraph callers /api/v1/predict    # Express 側 handler

cd ../ml-service
codegraph callers /predict            # FastAPI 側 handler

# ⑤ 驗證 handler 的完整呼叫路徑
codegraph explore "/api/v1/predict 從 Express route → middleware → proxy → FastAPI handler 的完整鏈"

# ⑥ 產出合約報告
codegraph query "" --kind route --json > /tmp/api-contract.json

關鍵觀察:route 節點是公開 API 的契約。用 route 節點把兩邊的「URL 集合」機械式比對,比人眼掃 routes 檔可靠——尤其 URL 數上百條時。

⚡ 效能 / 品質 / 安全深度

面向考量實務建議
效能route 節點的偵測時間framework resolver 的 detect() 在檔案存在前就跑一次;indexAll 後必須 runPostExtract() 重跑偵測
品質convention-heavy 框架的覆蓋率天花板Spring 83.3%、Django 74.1%——框架用 DI/反射時 route 節點抓不到,這是誠實的靜態分析限制
安全route 節點洩漏 API 結構圖譜在本機 SQLite;route 節點只顯示 URL 模式,不含認證/授權邏輯

📄 文件間比較對照表

面向本文(框架路由)相關文差異說明
17 個框架每個框架認得的路由寫法語言支援框架路由講「URL→handler」連結,語言支援講「語言→解析器」對應
route 節點references 邊的產生時機運作原理框架路由講 resolution 階段的框架特定行為
覆蓋率在 canonical app 上實測的數字效能基準框架覆蓋率是「解析品質」,效能基準是「使用效率」
detect() 時機indexAll 後必須 runPostExtract架構總覽框架路由講 resolver 行為,架構頁講 resolution 層角色

✅ 進階互動式檢核清單

- [ ] 能用 codegraph query --kind route 找出專案中所有 route 節點 - [ ] 能從 URL 追蹤到 handler 再到 service 層(完整的請求處理鏈) - [ ] 能解釋為什麼 convention-heavy 框架(Spring/Django)的 route 覆蓋率較低 - [ ] 能用 route 節點做跨框架的 API 合約比對(找出斷裂的 URL) - [ ] 能解釋 detect() 與 runPostExtract() 的時序關係 - [ ] 能區分「顯式路由」(Express/FastAPI)與「慣例路由」(Django/Rails)的解析差異