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.
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.
# 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.
# 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:
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
- Open http://localhost:8787. You should see “Hello World!”
- 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:
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.