Lộ trình đang học Current learning path

Developer Platform Developer Platform

Build và deploy apps trên Cloudflare. Build and deploy apps on Cloudflare.

Về trang lộ trình Track home

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)

Ví dụ chính thức từ Cloudflare Resources — gợi ý theo chủ đề bài học trong lộ trình này. Official examples from Cloudflare Resources — matched to this lesson within this path.

Tutorial Tutorial Developer Platform Developer Platform

Thêm site Add a site

Tóm tắt thực hành: Thêm site trên Cloudflare. This tutorial uses Pulumi infrastructure as code (IaC) to familiarize yourself with the resource management lifecycle.

This tutorial uses Pulumi infrastructure as code (IaC) to familiarize yourself with the resource management lifecycle.

Tìm hiểu thêm Learn more

Xem thêm ví dụ trong lộ trình → More examples in this path →

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

Học xong hoặc muốn đổi hướng? Finished or want a different path?

Ba lộ trình độc lập — mỗi lộ trình chỉ gồm bài học và tài liệu trong phạm vi đó. Chọn lộ trình khác khi sẵn sàng, không cần học song song. Three independent paths — each includes only lessons and materials for that scope. Switch when you are ready; no need to study paths in parallel.

Chưa chắc — làm bài chọn lộ trình Not sure — use the path selector · So sánh cả ba lộ trình Compare all three paths