CodeGraph 偵測 web framework 的路由檔案,產出 route 節點,用 references 邊連結到對應的 handler class / 函式。你查某個 view / controller 的 callers 時,會順帶看到綁定它的 URL 模式——「這支 API 誰在呼叫?」變成「這支 API 對應哪條 URL、被誰呼叫」一次回答。
| Framework | 認得的寫法 |
|---|---|
| Django | path()、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 |
| Express | app.get(...)、router.post(...) 帶 middleware chain |
| NestJS | @Controller + @Get/@Post/…、GraphQL @Resolver + @Query/@Mutation、@MessagePattern/@EventPattern、@SubscribeMessage |
| Laravel | Route::get()、Route::resource()、Controller@action、tuple syntax |
| Drupal | *.routing.yml routes(_controller、_form、entity handlers);hook_* 在 .module/.theme/.install/.inc |
| Rails | get '/x', to: 'users#index'、hash-rocket => syntax |
| Spring | @GetMapping、@PostMapping、@RequestMapping on methods |
| Play | GET/POST/… verb routes in conf/routes → Controller.method actions(Scala + Java) |
| Gin / chi / gorilla / mux | r.GET(...)、router.HandleFunc(...) |
| Axum / actix / Rocket | .route("/x", get(handler)) |
| ASP.NET | [HttpGet("/x")] attributes on action methods |
| Vapor | app.get("x", use: handler) |
| React Router / SvelteKit | Route component nodes |
| Vue Router / Nuxt | pages/ file-based routes、server/api/ endpoints、route middleware |
| Astro | src/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)——它在自己身上也偵測到了框架路由。
框架感知路由是 CodeGraph 的「超能力」——它能自動連結「URL 端點」與「處理函式」,這是純靜態分析做不到的。
支援的框架分為兩類:
# 步驟 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 補充 |
codegraph query --kind route 找到所有路由前面的範例是在單一框架內追蹤請求。這裡是遷移場景:把一個 Rails app 搬到 FastAPI,上線前要確認「每條舊 URL 都有新對應」,且 handler 對齊。這是 route 節點最被低估的用法。
# ① 在舊 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。
| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| 找不到某條已知存在的路由 | 寫法不在支援範圍,或 detect() 未重跑 | 確認框架支援清單與寫法慣例;檢查 runPostExtract 有執行 |
| route 連到錯的 handler | 名稱衝突或慣例誤判 | codegraph node 手動驗證,必要時改寫法或補 resolver |
| 相同路由重複出現多次 | include()/blueprint 被展開多次 | 檢查是否有重複 import 或 prefix 疊加 |
| 覆蓋率偏低且無法改善 | 框架用 reflection/DI | 接受靜態分析天花板;用 include 補已知入口 |
| 動態路由(URL 由 DB/config 產生)沒進圖 | 非靜態字面,無法解析 | 這屬解析極限,需手動補節點或文件說明 |
/users/:id vs FastAPI /users/{id})會讓 comm 誤判為斷裂。設計一個正規化規則,並說明哪種差異該忽略、哪種該人工審。Controller@action)。resolver 要如何驗證該字串真的指向存在的 method?如果指向不存在的 method,你希望圖譜怎麼表示——斷掉的邊、警告、還是直接不建邊?一個同時用 Express(Node.js)與 FastAPI(Python)的微服務架構,要確認「每個 API 端點都有對應的 route 節點」且「handler 邏輯正確連結」:
# ① 分別索引兩個服務
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)的解析差異