Phần 2: API trên edge Part 2: APIs on the edge · Bài 2/2 Lesson 2/2

Worker độc lập khi cần routing phức tạp Standalone Worker for complex routing

Dùng Worker khi cần middleware, auth edge, hoặc nhiều hostname. Worker là đơn vị compute linh hoạt nhất. Use a Worker when you need middleware, edge auth, or multiple hostnames. Workers are the most flexible compute unit.

Standalone Worker for complex routing
Minh họa từ Cloudflare Reference Architecture (developers.cloudflare.com) Illustration from Cloudflare Reference Architecture (developers.cloudflare.com)

Các bước thực hiện Step-by-step

  1. `wrangler init` hoặc dashboard Create Worker. `wrangler init` or dashboard Create Worker.
  2. Viết fetch handler: routing theo pathname/hostname. Write fetch handler: route by pathname/hostname.
  3. Deploy `wrangler deploy` — gán custom domain hoặc route `*.example.com/*`. Deploy with `wrangler deploy` — attach custom domain or route `*.example.com/*`.
  4. Dùng `wrangler tail` khi debug. Use `wrangler tail` when debugging.

Giải thích chi tiết Detailed explanation

Worker độc lập phù hợp middleware toàn zone, BFF, hoặc multi-tenant routing — linh hoạt hơn Pages Functions. Standalone Workers suit zone-wide middleware, BFFs, or multi-tenant routing — more flexible than Pages Functions.

Lưu ý (best practices) Note (best practices)

Custom domain: Worker là origin, DNS/SSL tự động. Route: Worker chạy trước origin — cần DNS record proxied (orange cloud); thiếu record sẽ gây ERR_NAME_NOT_RESOLVED. Custom domain: the Worker is the origin with automatic DNS/SSL. Route: the Worker runs in front of an origin — you need a proxied DNS record (orange cloud); missing records cause ERR_NAME_NOT_RESOLVED.

Nguồn: Source: Workers Best Practices Workers Best Practices

Ví dụ triển khai (Cloudflare Resources) Deployment examples (Cloudflare Resources)

Tutorial, solution guide và reference từ developers.cloudflare.com/resources ↗ — gợi ý theo chủ đề bài học. Tutorials, solution guides, and reference docs from developers.cloudflare.com/resources ↗ — matched to this lesson topic.

Duyệt toàn bộ catalog → Browse full catalog →

Tài liệu Cloudflare Developers Cloudflare Developer docs

Cloudflare API (tham khảo) Cloudflare API (reference)

Dùng khi tự động hóa bước này qua script hoặc Terraform — cần API token phù hợp. Use when automating this step via script or Terraform — requires an appropriate API token.

developers.cloudflare.com/api ↗

Sơ đồ kiến trúc (Cloudflare Docs) Architecture diagrams (Cloudflare Docs)

Figure 1:  Traditional single-region architecture

Serverless global APIs Serverless global APIs

An example architecture of a serverless API on Cloudflare and aims to illustrate how different compute and data products could interact with each other. An example architecture of a serverless API on Cloudflare and aims to illustrate how different compute and data products could interact with each other.

Thuật ngữ: Concepts: Workers · D1 · R2 · Global API · Edge compute

Sơ đồ chính thức ↗ Official diagram ↗ · Serverless Serverless

Sản phẩm liên quan Related products