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

Pages Functions cho route `/api/*` Pages Functions for `/api/*` routes

Đặt handler trong `functions/` để xử lý form, webhook, proxy nhẹ. Phù hợp logic gắn với site Pages. Put handlers in `functions/` for forms, webhooks, light proxying. Good for logic tied to your Pages site.

Pages Functions for `/api/*` routes
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. Tạo thư mục `functions/` cạnh output static. Create a `functions/` folder beside static output.
  2. Thêm `functions/api/hello.ts` export `onRequestGet`. Add `functions/api/hello.ts` exporting `onRequestGet`.
  3. Deploy — gọi `https://<site>/api/hello`. Deploy — call `https://<site>/api/hello`.
  4. Binding D1/KV trong Pages project settings nếu cần. Bind D1/KV in Pages project settings if needed.

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

Pages Functions chạy trên Workers runtime — API gắn với site, không cần server riêng. Pages Functions run on the Workers runtime — APIs colocated with your site, no separate server.

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

Luôn `await` hoặc `ctx.waitUntil()` mọi Promise — nếu không, Worker có thể kết thúc trước khi tác vụ nền hoàn tất. Always `await` or use `ctx.waitUntil()` for every Promise — otherwise the Worker may terminate before background work completes.

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

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

Figure 1: Cloudflare Developer Platform

Fullstack applications Fullstack applications

Ví dụ fullstack trên Developer Platform — Workers/Pages, storage và AI services trong một kiến trúc thực tế. A practical example of how these services come together in a real fullstack application architecture.

Thuật ngữ: Concepts: Workers · Pages · D1 · R2 · KV · Durable Objects

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

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