Cloudflare Workers Cloudflare Workers Cloudflare Workers · Lab 1/7 Lab 1/7 Lab 1/7 · 10 phút · 10 min · 10 នាទី

01

Bắt đầu Getting Started ចាប់ផ្តើម

Scaffold project Bookmark API, hiểu runtime Workers, và test Worker đầu tiên trên local. Scaffold the Bookmark API project, understand the Workers runtime, and test your first Worker locally. Scaffold project Bookmark API យល់ runtime Workers និង test Worker ដំបូងលើ local។

Nội dung bước (lệnh, code) giữ nguyên tiếng Anh từ nguồn chính thức. Step body (commands, code) stays in English from the official source. ខ្លឹមសារជំហាន (ពាក្យបញ្ជា និង code) រក្សាភាសាអង់គ្លេសពីប្រភពផ្លូវការ។ labs.cloudflare.dev ↗

Cần trước Prerequisites តម្រូវការជាមុន

  • Node.js v20+ Node.js v20+ Node.js v20+
  • Tài khoản Cloudflare Cloudflare Account គណនី Cloudflare
  • Terminal / dòng lệnh Terminal/Command Line Terminal / ពាក្យបញ្ជា
  • Biết JavaScript cơ bản Basic JavaScript knowledge ចេះ JavaScript មូលដ្ឋាន

Bạn sẽ làm được Learning objectives គោលបំណងសិក្សា

  • Scaffold project Workers và hiểu cấu trúc file được tạo Scaffold a Workers project and understand the generated file structure Scaffold project Workers និងយល់រចនាសម្ព័ន្ធ file ដែលបានបង្កើត
  • Giải thích fetch handler nhận và trả HTTP request Explain how the fetch handler receives and responds to HTTP requests ពន្យល់ថា fetch handler ទទួល និងឆ្លើយ HTTP request យ៉ាងណា
  • Dùng Wrangler để chạy, test, sửa Worker trên local Use Wrangler to run, test, and iterate on a Worker locally ប្រើ Wrangler ដើម្បីរត់ test និងកែ Worker លើ local

Xem: Cloudflare Workers 101 Watch: Cloudflare Workers 101 មើល៖ Cloudflare Workers 101

Before diving in, watch this overview of Workers architecture and concepts.

Before diving in, watch this overview of Workers architecture and concepts.

Before diving in, watch this overview of Workers architecture and concepts.

Cloudflare Workers 101 Tutorial

Bước 1: Đăng nhập Cloudflare Step 1: Authenticate with Cloudflare ជំហាន 1: ចូល Cloudflare

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
A verified connection between your local machine and your Cloudflare account.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
Later steps create KV namespaces, D1 databases, and deploy your Worker. All of these require authentication. Setting this up first prevents confusing errors later.
bash
# Log in to your Cloudflare account (opens a browser window)
npx wrangler login

# Verify you are authenticated
npx wrangler whoami

You should see your account name and ID. If not, run npx wrangler login again.

You should see your account name and ID. If not, run npx wrangler login again.

You should see your account name and ID. If not, run npx wrangler login again.

Bước 2: Tạo project Bookmark API Step 2: Create the Bookmark API Project ជំហាន 2: បង្កើត project Bookmark API

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
A new Cloudflare Worker project called bookmark-api running locally.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
Every Workers project starts with a scaffold. This gives you the project structure, TypeScript config, and local dev server.
bash
# Create the project
npm create cloudflare@latest -- bookmark-api --type hello-world --ts

# Navigate into the project
cd bookmark-api

# Start the local dev server
npm run dev

Running npm create cloudflare@latest will trigger a series of interactive prompts (e.g. package manager, git initialization, deploy). Accept the defaults for each prompt to match the configuration used throughout this workshop.

Running npm create cloudflare@latest will trigger a series of interactive prompts (e.g. package manager, git initialization, deploy). Accept the defaults for each prompt to match the configuration used throughout this workshop.

Running npm create cloudflare@latest will trigger a series of interactive prompts (e.g. package manager, git initialization, deploy). Accept the defaults for each prompt to match the configuration used throughout this workshop.

  • npm not found: Install Node.js v20+ from nodejs.org
  • Template not found: Update npm: npm install -g npm@latest
  • Port 8787 busy: Use npx wrangler dev --port 8788
  • Build errors: Delete node_modules and run npm install again

Bước 3: Đọc code Step 3: Understand the Code ជំហាន 3: អាន code

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
An understanding of how the fetch handler processes HTTP requests.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
The fetch handler is the entry point for every request your Worker receives. Understanding its parameters is fundamental.

Open src/index.ts:

Open src/index.ts:

Open src/index.ts:

typescript
export default {
  async fetch(request, env, ctx): Promise<Response> {
    return new Response('Hello World!');
  },
} satisfies ExportedHandler<Env>;

Every Cloudflare Worker exports a default object with handler functions. The fetch handler is called for every HTTP request.

Every Cloudflare Worker exports a default object with handler functions. The fetch handler is called for every HTTP request.

Every Cloudflare Worker exports a default object with handler functions. The fetch handler is called for every HTTP request.

Bước 4: Test và sửa Step 4: Test and Modify ជំហាន 4: Test និងកែ

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
Confidence that the local dev server works and hot reloading updates your Worker instantly.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
Fast local iteration is essential. You will be modifying this file many times throughout the workshop.

Test trên trình duyệt Test in the browser Test លើ browser

  1. Open http://localhost:8787. You should see “Hello World!”
  2. Try http://localhost:8787/anything. Same response (no routing yet)

Sửa một chỗ Make a change កែចំណុចមួយ

Update src/index.ts to return JSON instead of plain text:

Update src/index.ts to return JSON instead of plain text:

Update src/index.ts to return JSON instead of plain text:

typescript
export default {
  async fetch(request, env, ctx): Promise<Response> {
    return Response.json({
      name: 'Bookmark API',
      version: '1.0.0',
      status: 'running'
    });
  },
} satisfies ExportedHandler<Env>;

Save the file. Wrangler picks up the change automatically. Refresh your browser to see JSON output.

Save the file. Wrangler picks up the change automatically. Refresh your browser to see JSON output.

Save the file. Wrangler picks up the change automatically. Refresh your browser to see JSON output.