Tạo toàn bộ danh sách ứng dụng Jamstack Build a todo list Jamstack application បង្កើតបញ្ជីការងារត្រូវធ្វើ Jamstack
Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page. មគ្គុទ្ទេសក៍លម្អិតធ្វើសមកាលពី docs Cloudflare — ផ្នែកនីមួយៗមានតំណទៅទីតាំងត្រូវគ្នានៅទំព័រផ្លូវការ។
← Danh mục ← Catalog ← បញ្ជីGiải thích nhanh Quick context បរិបទរហ័ស
Thuộc Developer Platform — thực hành triển khai code, API và dữ liệu trên edge/serverless. Tutorial «Xây dựng todo list Jamstack ứng dụng» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 6 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.
This tutorial explains how to build a todo list application using HTML, CSS, and JavaScript.
ការបង្រៀននេះពន្យល់ពីរបៀបបង្កើតកម្មវិធីបញ្ជីការងារត្រូវធ្វើដោយប្រើ HTML, CSS និង JavaScript ។
Lưu ý Note ចំណាំ
Lưu ý trước khi làm Notes before you start ចំណាំមុនពេលចាប់ផ្តើម
- Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức.
- Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ.
- Yêu cầu trước (từ docs): All of the tutorials assume you have already đã hoàn thành hướng dẫn Get started, which gets you set up with a Cloudflare Workers account, C3 ↗, and Wrangler.
- Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production.
- This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
- Open the Official docs link below for CLI commands, code snippets, and full screenshots.
- Prerequisites (from docs): All of the tutorials assume you have already completed the Get started guide, which gets you set up with a Cloudflare Workers account, C3 ↗, and Wrangler.
- Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.
- នេះគឺជាការសង្ខេបនៅលើ Orange Cloud Learning Hub — វាមិនជំនួសឯកសារផ្លូវការទេ។
- បើកតំណឯកសារផ្លូវការខាងក្រោមសម្រាប់ពាក្យបញ្ជា CLI កូដ snippets និងរូបថតអេក្រង់ពេញ។
- តម្រូវការជាមុន (ពីឯកសារ)៖ ការបង្រៀនទាំងអស់សន្មត់ថាអ្នកបានបញ្ចប់ការណែនាំ Get started រួចហើយ ដែលធ្វើឲ្យអ្នករៀបចំជាមួយគណនី Cloudflare Workers C3 ↗ និង Wrangler ។
- Cloudflare ឯកសារផ្លាស់ប្តូរជាញឹកញាប់ — ផ្ទៀងផ្ទាត់កាលបរិច្ឆេទដែលបានពិនិត្យចុងក្រោយនៅលើទំព័រផ្លូវការមុនពេលប្រើប្រាស់ផលិតកម្ម។
Tài liệu gốc — rà soát lần cuối: about 2 years ago Official docs — last reviewed: about 2 years ago ឯកសារផ្លូវការ — ពិនិត្យចុងក្រោយ: about 2 years ago
Overview Overview ទិដ្ឋភាពទូទៅ
Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «ទិដ្ឋភាពទូទៅ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
- Cách xây dựng với Workers cho phép bạn tập trung vào việc viết mã và vận chuyển các sản phẩm hoàn thành.
- Làm thế nào việc thêm Workers KV làm cho hướng dẫn này một giới thiệu tuyệt vời để xây dựng đầy đủ, ứng dụng dựa trên dữ liệu.
- How building with Workers makes allows you to focus on writing code and ship finished products.
- How the addition of Workers KV makes this tutorial a great introduction to building full, data-driven applications.
- របៀបបង្កើតជាមួយ Workers អនុញ្ញាតឱ្យអ្នកផ្តោតលើការសរសេរកូដ និង ship ផលិតផលដែលបានបញ្ចប់។
- របៀបដែលការបន្ថែមនៃ Workers KV ធ្វើឱ្យការបង្រៀននេះក្លាយជាការណែនាំដ៏ល្អក្នុងការបង្កើតកម្មវិធីដែលដំណើរការដោយទិន្នន័យពេញលេញ។
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)
Trước khi bạn bắt đầu Before you start មុនពេលអ្នកចាប់ផ្តើម
Phần «Trước khi bắt đầu» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Before you start" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «មុនពេលអ្នកចាប់ផ្តើម» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)
1.Điều Tạo dự án mới Workers 1. Create a new Workers project 1. បង្កើតគម្រោង Workers ថ្មី។
Phần «Tạo Workers dự án mới» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "1. Create a new Workers project" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «1. បង្កើតគម្រោង Workers ថ្មី។» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
todos. Trong hướng dẫn này, bạn sẽ sử dụng mẫu Hello World mặc định để tạo một dự án Workers.todos. In this tutorial, you will use the default Hello World template to create a Workers project.todos។ នៅក្នុងមេរៀននេះ អ្នកនឹងប្រើគំរូ Hello World លំនាំដើមដើម្បីបង្កើតគម្រោង Workers។npm create cloudflare@latest -- todos yarn create cloudflare todos pnpm create cloudflare@latest todos - Đối với Bạn muốn bắt đầu với gì?, chọn
Hello World example. - Đối với bạn muốn sử dụng mẫu nào?, chọn
Worker only. - Đối với Ngôn ngữ nào bạn muốn sử dụng?, chọn
JavaScript. - Đối với Do you want to use git for version control?, chọn
Yes. - Đối với Bạn muốn triển khai ứng dụng của mình?, chọn
No(chúng tôi sẽ thực hiện một số thay đổi trước khi triển khai).
- For What would you like to start with?, choose
Hello World example. - For Which template would you like to use?, choose
Worker only. - For Which language do you want to use?, choose
JavaScript. - For Do you want to use git for version control?, choose
Yes. - For Do you want to deploy your application?, choose
No(we will be making some changes before deploying).
- សម្រាប់ តើអ្នកចង់ចាប់ផ្តើមជាមួយអ្វី? សូមជ្រើសរើស
Hello World example។ - សម្រាប់ តើគំរូមួយណាដែលអ្នកចង់ប្រើ? សូមជ្រើសរើស
Worker only។ - សម្រាប់ តើភាសាមួយណាដែលអ្នកចង់ប្រើ? សូមជ្រើសរើស
JavaScript។ - សម្រាប់ តើអ្នកចង់ប្រើ git សម្រាប់ការគ្រប់គ្រងកំណែដែរឬទេ? សូមជ្រើសរើស
Yes។ - សម្រាប់ តើអ្នកចង់ដាក់ឱ្យប្រើប្រាស់កម្មវិធីរបស់អ្នកទេ? សូមជ្រើសរើស
No(យើងនឹងធ្វើការកែប្រែខ្លះមុនពេលដាក់ឱ្យប្រើប្រាស់)។
cd todos todos Workers mới của bạn, index.js đại diện cho điểm đầu vào ứng dụng Cloudflare Workers của bạn.todos Worker project directory, index.js represents the entry point to your Cloudflare Workers application.todos ថតគម្រោងអ្នកធ្វើការថ្មីរបស់អ្នក index.js តំណាងឱ្យចំណុចចូលនៃកម្មវិធី Cloudflare Workers របស់អ្នក។export default {
async fetch(request, env, ctx) {
return new Response("Hello World!");
},
}; index.js mặc định của bạn, bạn có thể thấy mẫu yêu cầu / phản hồi đó trong action. fetch xây dựng một Response mới với nội dung 'Hello World!'.index.js file, you can see that request/response pattern in action. The fetch constructs a new Response with the body text 'Hello World!'.index.js លំនាំដើមរបស់អ្នក អ្នកអាចឃើញគំរូសំណើ/ការឆ្លើយតបនោះនៅក្នុងសកម្មភាព។ fetch បង្កើត Response ថ្មីជាមួយនឹងអត្ថបទ 'Hello World!'។request, Công nhân trả về câu trả lời mới được xây dựng cho khách hàng. Nhân viên của bạn sẽ phục vụ các câu trả lời mới trực tiếp từ Cloudflare's global network ↗ thay vì tiếp tục đến máy chủ gốc của bạn. Một máy chủ tiêu chuẩn sẽ chấp nhận yêu cầu và trả lời. Cloudflare Workers cho phép bạn trả lời bằng cách xây dựng câu trả lời trực tiếp trên mạng toàn cầu Cloudflare.request, the Worker returns the newly constructed response to the client. Your Worker will serve new responses directly from Cloudflare's global network ↗ instead of continuing to your origin server. A standard server would accept requests and return responses. Cloudflare Workers allows you to respond by constructing responses directly on the Cloudflare global network.request កម្មករនិយោជិតនឹងបញ្ជូនការឆ្លើយតបដែលបានបង្កើតថ្មីទៅ client។ បុគ្គលិករបស់អ្នកនឹងបម្រើការឆ្លើយតបថ្មីដោយផ្ទាល់ពី Cloudflare បណ្តាញសកលរបស់ ↗ ជំនួសឱ្យការបន្តទៅកាន់ម៉ាស៊ីនមេដើមរបស់អ្នក។ ម៉ាស៊ីនមេស្តង់ដារនឹងទទួលយកសំណើ និងឆ្លើយតបមកវិញ។ Cloudflare Workers អនុញ្ញាតឱ្យអ្នកឆ្លើយតបដោយបង្កើតការឆ្លើយតបដោយផ្ទាល់នៅលើបណ្តាញសកល Cloudflare ។Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)
2.Đối với Xem chi tiết dự án 2. Review project details 2. ពិនិត្យព័ត៌មានលម្អិតអំពីគម្រោង
Phần «Review dự án details» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "2. Review project details" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «2. ពិនិត្យព័ត៌មានលម្អិតអំពីគម្រោង» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
npm gói và async/await ↗ chức năng để xây dựng ứng dụng của bạn. Ngoài việc viết Workers, bạn có thể sử dụng Workers để build full applications bằng cách sử dụng các công cụ và quy trình tương tự như trong hướng dẫn này.npm packages, and async/await ↗ functions to build your application. In addition to writing Workers, you can use Workers to build full applications using the same tooling and process as in this tutorial.- Viết dữ liệu thành KV.
- Hiển thị dữ liệu từ KV.
- Thêm tất cả từ ứng dụng UI.
- Write data to KV.
- Rendering data from KV.
- Adding todos from the application UI.
- សរសេរទិន្នន័យទៅ KV ។
- កំពុងបង្ហាញទិន្នន័យពី KV ។
- ការបន្ថែមអ្វីដែលត្រូវធ្វើពី UI កម្មវិធី។
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)
3 Cái Viết dữ liệu thành KV 3. Write data to KV 3. សរសេរទិន្នន័យទៅ KV
Phần «Write data to KV» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "3. Write data to KV" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «3. សរសេរទិន្នន័យទៅ KV» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
TODOS với kv namespace create command và nhận ID không gian tên liên quan bằng cách chạy lệnh sau trong thiết bị đầu cuối của bạn:TODOS with the kv namespace create command and get the associated namespace ID by running the following command in your terminal:TODOS ជាមួយ kv namespace បង្កើតពាក្យបញ្ជា និងទទួលបាន associated namespace ID ដោយដំណើរការពាក្យបញ្ជាខាងក្រោមនៅក្នុងស្ថានីយរបស់អ្នក៖npx wrangler kv namespace create "TODOS" --preview --preview để tương tác với không gian tên xem trước thay vì không gian tên sản xuất. Không gian tên có thể được thêm vào ứng dụng của bạn bằng cách xác định chúng trong cấu hình Wrangler của bạn. Sao chép ID không gian tên mới được tạo, và trong Wrangler configuration file của bạn, xác định một phím kv_namespaces để thiết lập không gian tên của bạn:--preview flag to interact with a preview namespace instead of a production namespace. Namespaces can be added to your application by defining them inside your Wrangler configuration. Copy your newly created namespace ID, and in your Wrangler configuration file, define a kv_namespaces key to set up your namespace:--preview ដើម្បីធ្វើអន្តរកម្មជាមួយ namespace ការមើលជាមុនជំនួសឱ្យ namespace ផលិតកម្ម។ Namespaces អាចត្រូវបានបន្ថែមទៅកម្មវិធីរបស់អ្នកដោយកំណត់ពួកវានៅក្នុងការកំណត់រចនាសម្ព័ន្ធ Wrangler របស់អ្នក។ ចម្លងលេខសម្គាល់ namespace ដែលទើបបង្កើតថ្មីរបស់អ្នក ហើយនៅក្នុង kv_namespaces គន្លឹះដើម្បីដំឡើងទំហំឈ្មោះរបស់អ្នក៖{
"kv_namespaces": [
{
"binding": "TODOS",
"id": "<YOUR_ID>",
"preview_id": "<YOUR_PREVIEW_ID>"
}
]
} [[kv_namespaces]]
binding = "TODOS"
id = "<YOUR_ID>"
preview_id = "<YOUR_PREVIEW_ID>" TODOS, bây giờ sẽ có sẵn bên trong cơ sở mã của bạn. Với điều đó, đã đến lúc hiểu KV API. Không gian tên KV có ba phương pháp chính mà bạn có thể sử dụng để giao diện với bộ nhớ cache của bạn: get, put và delete.TODOS, will now be available inside of your codebase. With that, it is time to understand the KV API. A KV namespace has three primary methods you can use to interface with your cache: get, put, and delete.TODOS ឥឡូវនេះនឹងមាននៅក្នុងមូលដ្ឋានកូដរបស់អ្នក។ ជាមួយនោះ វាដល់ពេលហើយដើម្បីស្វែងយល់អំពី __TERM60__5__0> KV namespace មានវិធីចម្បងបីដែលអ្នកអាចប្រើដើម្បីប៉ះជាមួយឃ្លាំងសម្ងាត់របស់អ្នក៖ get put និង delete។put. Ví dụ sau đây xác định một đối tượng defaultData thay vì một mảng các mục todo. Bạn có thể muốn lưu trữ siêu dữ liệu và thông tin khác bên trong đối tượng bộ nhớ cache này sau này. Với đối tượng dữ liệu đó, hãy sử dụng JSON.stringify để thêm một chuỗi vào bộ nhớ cache:put method. The following example defines a defaultData object instead of an array of todo items. You may want to store metadata and other information inside of this cache object later on. Given that data object, use JSON.stringify to add a string into the cache:put ។ ឧទាហរណ៍ខាងក្រោមកំណត់វត្ថុ defaultData ជំនួសឱ្យអារេនៃធាតុដែលត្រូវធ្វើ។ អ្នកប្រហែលជាចង់រក្សាទុកទិន្នន័យមេតា និងព័ត៌មានផ្សេងទៀតនៅខាងក្នុងនៃវត្ថុឃ្លាំងសម្ងាត់នេះនៅពេលក្រោយ។ ដោយបានផ្ដល់វត្ថុទិន្នន័យនោះ សូមប្រើ JSON.stringify ដើម្បីបន្ថែមខ្សែអក្សរទៅក្នុងឃ្លាំងសម្ងាត់៖export default {
async fetch(request, env, ctx) {
const defaultData = {
todos: [
{
id: 1,
name: "Finish the Cloudflare Workers blog post",
completed: false,
},
],
};
await env.TODOS.put("data", JSON.stringify(defaultData));
return new Response("Hello World!");
},
}; defaultData làm nguồn dữ liệu cho bây giờ (nó nên được thiết lập trong tương lai) và viết nó vào bộ nhớ cache để sử dụng trong tương lai. Sau khi phá vỡ mã thành một vài chức năng để đơn giản hóa, kết quả trông như thế này:defaultData as the data source for now (it should be set in the future) and write it to the cache for future use. After breaking out the code into a few functions for simplicity, the result looks like this:defaultData ជាប្រភពទិន្នន័យសម្រាប់ពេលនេះ (វាគួរតែត្រូវបានកំណត់នាពេលអនាគត) ហើយសរសេរវាទៅក្នុងឃ្លាំងសម្ងាត់សម្រាប់ការប្រើប្រាស់នាពេលអនាគត។ បន្ទាប់ពីបំបែកកូដទៅជាមុខងារមួយចំនួនសម្រាប់ភាពសាមញ្ញ លទ្ធផលមើលទៅដូចនេះ៖export default {
async fetch(request, env, ctx) {
const defaultData = {
todos: [
{
id: 1,
name: "Finish the Cloudflare Workers blog post",
completed: false,
},
],
};
const setCache = (data) => env.TODOS.put("data", data);
const getCache = () => env.TODOS.get("data");
let data;
const cache = await getCache();
if (!cache) {
await setCache(JSON.stringify(defaultData));
data = defaultData;
} else {
data = JSON.parse(cache);
}
return new Response(JSON.stringify(data));
},
}; Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)
Hiển thị dữ liệu từ KV Render data from KV បង្ហាញទិន្នន័យពី KV
Phần «Render data từ KV» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Render data from KV" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «បង្ហាញទិន្នន័យពី KV» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
html mới trong kịch bản Workers của bạn và sử dụng nó để xây dựng một mẫu tĩnh HTML mà bạn có thể phục vụ cho khách hàng. Trong fetch, xây dựng một Response mới với tiêu đề Content-Type: text/html và phục vụ nó cho khách hàng:html variable in your Workers script and use it to build up a static HTML template that you can serve to the client. In fetch, construct a new Response with a Content-Type: text/html header and serve it to the client:html នៅក្នុង Workers script របស់អ្នក ហើយប្រើវាដើម្បីបង្កើតគំរូ HTML ឋិតិវន្ត ដែលអ្នកអាចបម្រើដល់ client ។ នៅក្នុង fetch បង្កើត Response ថ្មីជាមួយនឹង Content-Type: text/html ហើយបម្រើវាទៅ client៖const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Todos</title>
</head>
<body>
<h1>Todos</h1>
</body>
</html>
`;
async fetch (request, env, ctx) {
// previous code
return new Response(html, {
headers: {
'Content-Type': 'text/html'
}
});
} div với một id của todos:div tag with an id of todos:div ជាមួយនឹង id នៃ todos៖const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Todos</title>
</head>
<body>
<h1>Todos</h1>
<div id="todos"></div>
</body>
</html>
`; <script> ở cuối nội dung cơ thể có mảng todos. Đối với mỗi todo trong mảng, tạo một yếu tố div và đính kèm nó vào yếu tố todos HTML:<script> element at the end of the body content that takes a todos array. For each todo in the array, create a div element and appends it to the todos HTML element:<script> នៅចុងបញ្ចប់នៃមាតិកាតួ ដែលយកអារេ todos។ សម្រាប់ todo នីមួយៗក្នុងអារេ បង្កើតធាតុ div ហើយបន្ថែមវាទៅ todos ធាតុ HTML៖const html = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Todos</title>
</head>
<body>
<h1>Todos</h1>
<div id="todos"></div>
</body>
<script>
window.todos = []
var todoContainer = document.querySelector("#todos")
window.todos.forEach(todo => {
var el = document.createElement("div")
el.textContent = todo.name
todoContainer.appendChild(el)
})
</script>
</html>
`; window.todos và hiển thị HTML dựa trên nó, nhưng bạn không thực sự thông qua bất kỳ dữ liệu nào từ KV. Để làm điều này, bạn sẽ cần phải thực hiện một số thay đổi.window.todos and render HTML based on it, but you have not actually passed in any data from KV. To do this, you will need to make a few changes.window.todos ហើយបង្ហាញ HTML ដោយផ្អែកលើវា ប៉ុន្តែអ្នកពិតជាមិនបានឆ្លងកាត់ទិន្នន័យណាមួយពី KV ទេ។ ដើម្បីធ្វើដូចនេះអ្នកនឹងត្រូវធ្វើការផ្លាស់ប្តូរមួយចំនួន។html của bạn sẽ thay đổi thành hàm. Chức năng này sẽ sử dụng đối số todos, sẽ chứa biến window.todos trong mẫu mã ở trên:html variable will change to a function. The function will take in a todos argument, which will populate the window.todos variable in the above code sample:html របស់អ្នកនឹងផ្លាស់ប្តូរទៅជាមុខងារមួយ។ មុខងារនឹងប្រើក្នុងអាគុយម៉ង់ todos ដែលនឹងបញ្ចូលអថេរ window.todos ក្នុងគំរូកូដខាងលើ៖const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
window.todos = ${todos}
var todoContainer = document.querySelector("#todos")
// ...
<script>
</html>
`; fetch, sử dụng dữ liệu KV được lấy để gọi hàm html và tạo ra hàm Response dựa trên nó:fetch, use the retrieved KV data to call the html function and generate a Response based on it:fetch ប្រើទិន្នន័យ KV ដែលបានទាញយក ដើម្បីហៅមុខងារ html ហើយបង្កើត Response ដោយផ្អែកលើវា៖async fetch (request, env, ctx) {
const body = html(JSON.stringify(data.todos).replace(/</g, '\\u003c'));
return new Response(body, {
headers: { 'Content-Type': 'text/html' },
});
} Bốn Thêm tất cả từ giao diện người dùng (UI) 4. Add todos from the user interface (UI) 4. បន្ថែមការងារត្រូវធ្វើពីចំណុចប្រទាក់អ្នកប្រើ (UI)
Phần «Thêm todos từ user interface (UI)» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "4. Add todos from the user interface (UI)" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «4. បន្ថែមការងារត្រូវធ្វើពីចំណុចប្រទាក់អ្នកប្រើ (UI)» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
env.TODOS.put(newData).env.TODOS.put(newData).env.TODOS.put(newData)។PUT đến /. Khi một cơ quan yêu cầu được nhận tại URL đó, Nhân viên sẽ gửi dữ liệu todo mới đến cửa hàng KV của bạn.PUT requests to /. When a request body is received at that URL, the Worker will send the new todo data to your KV store.PUT ទៅកាន់ /។ នៅពេលដែលអង្គភាពសំណើត្រូវបានទទួលនៅ URL នោះ កម្មករនឹងបញ្ជូនទិន្នន័យការងារត្រូវធ្វើថ្មីទៅកាន់ហាង KV របស់អ្នក។fetch: nếu phương pháp yêu cầu là PUT, nó sẽ lấy cơ thể yêu cầu và cập nhật bộ nhớ cache.fetch: if the request method is a PUT, it will take the request body and update the cache.fetch៖ ប្រសិនបើវិធីសាស្ត្រស្នើសុំគឺ PUT វានឹងយកតួសំណើ ហើយធ្វើបច្ចុប្បន្នភាពឃ្លាំងសម្ងាត់។export default {
async fetch(request, env, ctx) {
const setCache = (data) => env.TODOS.put("data", data);
if (request.method === "PUT") {
const body = await request.text();
try {
JSON.parse(body);
await setCache(body);
return new Response(body, { status: 200 });
} catch (err) {
return new Response(err, { status: 500 });
}
}
// previous code
},
}; PUT và đóng gói phần còn lại của mã trong một khối try...catch. Đầu tiên, phân tích cơ thể của yêu cầu đến, đảm bảo rằng đó là JSON, trước khi bạn cập nhật bộ nhớ cache với dữ liệu mới và trả lại cho người dùng. Nếu điều gì đó không ổn, hãy trả về mã trạng thái 500. Nếu đường dẫn được nhấn bằng phương pháp HTTP khác với PUT – ví dụ, POST hoặc DELETE – trả về lỗi 404.PUT and wrap the remainder of the code in a try...catch block. First, parse the body of the request coming in, ensuring that it is JSON, before you update the cache with the new data and return it to the user. If anything goes wrong, return a 500 status code. If the route is hit with an HTTP method other than PUT — for example, POST or DELETE — return a 404 error.PUT ហើយរុំកូដដែលនៅសល់ក្នុងប្លុក try...catch។ ដំបូង ញែកតួនៃសំណើដែលចូលមក ដោយធានាថាវាជា JSON មុនពេលអ្នកធ្វើបច្ចុប្បន្នភាពឃ្លាំងសម្ងាត់ជាមួយនឹងទិន្នន័យថ្មី ហើយប្រគល់វាទៅអ្នកប្រើប្រាស់វិញ។ ប្រសិនបើមានអ្វីខុស សូមត្រឡប់លេខកូដស្ថានភាព 500។ ប្រសិនបើផ្លូវត្រូវបានវាយប្រហារដោយវិធីសាស្ត្រ HTTP ផ្សេងក្រៅពី PUT — ឧទាហរណ៍ POST ឬ DELETE — ត្រឡប់ 4const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<div>
<input type="text" name="name" placeholder="A new todo"></input>
<button id="create">Create</button>
</div>
<!-- existing script -->
</html>
`; PUT đến / và gửi todo.PUT to / and submit the todo.PUT ទៅ ando>tutorial/-code>const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
// Existing JavaScript code
var createTodo = function() {
var input = document.querySelector("input[name=name]")
if (input.value.length) {
todos = [].concat(todos, {
id: todos.length + 1,
name: input.value,
completed: false,
})
fetch("/", {
method: "PUT",
body: JSON.stringify({ todos: todos }),
})
}
}
document.querySelector("#create").addEventListener("click", createTodo)
</script>
</html>
`; populateTodos, và gọi nó khi trang tải và khi yêu cầu bộ nhớ cache đã kết thúc:populateTodos, and calling it when the page loads and when the cache request has finished:populateTodos ហើយហៅវានៅពេលទំព័រផ្ទុក និងនៅពេលដែលសំណើឃ្លាំងសម្ងាត់បានបញ្ចប់៖const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
var populateTodos = function() {
var todoContainer = document.querySelector("#todos")
todoContainer.innerHTML = null
window.todos.forEach(todo => {
var el = document.createElement("div")
el.textContent = todo.name
todoContainer.appendChild(el)
})
}
populateTodos()
var createTodo = function() {
var input = document.querySelector("input[name=name]")
if (input.value.length) {
todos = [].concat(todos, {
id: todos.length + 1,
name: input.value,
completed: false,
})
fetch("/", {
method: "PUT",
body: JSON.stringify({ todos: todos }),
})
populateTodos()
input.value = ""
}
}
document.querySelector("#create").addEventListener("click", createTodo)
</script>
`; 5 Cập nhật tất cả từ ứng dụng UI 5. Update todos from the application UI 5. ធ្វើបច្ចុប្បន្នភាពអ្វីដែលត្រូវធ្វើពី UI កម្មវិធី
Phần «Cập nhật todos từ ứng dụng UI» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "5. Update todos from the application UI" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «5. ធ្វើបច្ចុប្បន្នភាពអ្វីដែលត្រូវធ្វើពី UI កម្មវិធី» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
createTodo của bạn. Thực hiện cập nhật trên một todo là một nhiệm vụ phía khách hàng hơn là một nhiệm vụ phía công nhân.createTodo function. Performing updates on a todo is more of a client-side task than a Worker-side one.createTodo របស់អ្នក។ ការអនុវត្តការអាប់ដេតលើការងារត្រូវធ្វើគឺជាកិច្ចការខាងភាគី cli ច្រើនជាងផ្នែកខាងកម្មករ។populateTodos có thể được cập nhật để tạo ra một div cho mỗi todo. Ngoài ra, di chuyển tên của todo vào một yếu tố con của div:populateTodos function can be updated to generate a div for each todo. In addition, move the name of the todo into a child element of that div:populateTodos អាចត្រូវបានអាប់ដេតដើម្បីបង្កើត div សម្រាប់ការងារនីមួយៗ។ លើសពីនេះ ផ្លាស់ទីឈ្មោះរបស់ការងារត្រូវធ្វើទៅជាធាតុកូននៃ div៖const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
var populateTodos = function() {
var todoContainer = document.querySelector("#todos")
todoContainer.innerHTML = null
window.todos.forEach(todo => {
var el = document.createElement("div")
var name = document.createElement("span")
name.textContent = todo.name
el.appendChild(name)
todoContainer.appendChild(el)
})
}
</script>
`; dataset tương ứng trong JavaScript là một cách hoàn hảo để thực hiện điều này. Khi bạn tạo thành phần div cho mỗi todo, bạn có thể đính kèm một thuộc tính dữ liệu được gọi là todo vào mỗi div:dataset method in JavaScript are a perfect way to implement this. When you generate your div element for each todo, you can attach a data attribute called todo to each div:dataset ដែលត្រូវគ្នានៅក្នុង JavaScript គឺជាវិធីដ៏ល្អឥតខ្ចោះដើម្បីអនុវត្តវា។ នៅពេលអ្នកបង្កើតធាតុ div របស់អ្នកសម្រាប់ការងារត្រូវធ្វើនីមួយៗ អ្នកអាចភ្ជាប់គុណលក្ខណៈទិន្នន័យហៅថា todo ទៅ div៖const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
var populateTodos = function() {
var todoContainer = document.querySelector("#todos")
todoContainer.innerHTML = null
window.todos.forEach(todo => {
var el = document.createElement("div")
el.dataset.todo = todo.id
var name = document.createElement("span")
name.textContent = todo.name
el.appendChild(name)
todoContainer.appendChild(el)
})
}
</script>
`; div cho một todo bây giờ có một thuộc tính dữ liệu đính kèm, trông như sau:div for a todo now has an attached data attribute, which looks like:div សម្រាប់ការងារត្រូវធ្វើឥឡូវនេះមានគុណលក្ខណៈទិន្នន័យដែលបានភ្ជាប់ ដែលមើលទៅដូច៖<div data-todo="1"></div>
<div data-todo="2"></div> const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
window.todos.forEach(todo => {
var el = document.createElement("div")
el.dataset.todo = todo.id
var name = document.createElement("span")
name.textContent = todo.name
var checkbox = document.createElement("input")
checkbox.type = "checkbox"
checkbox.checked = todo.completed ? 1 : 0
el.appendChild(checkbox)
el.appendChild(name)
todoContainer.appendChild(el)
})
</script>
`; completeTodo làm người nghe sự kiện vào sự kiện click. Bên trong hàm, kiểm tra phần tử hộp kiểm, tìm nguyên mẫu của nó (Todo div), và sử dụng thuộc tính dữ liệu todo của nó để tìm todo tương ứng trong mảng dữ liệu. Bạn có thể chuyển trạng thái hoàn thành, cập nhật các thuộc tính của nó và thuê lại UI:completeTodo function as an event listener on the click event. Inside the function, inspect the checkbox element, find its parent (the todo div), and use its todo data attribute to find the corresponding todo in the data array. You can toggle the completed status, update its properties, and rerender the UI:completeTodo ជាអ្នកស្តាប់ព្រឹត្តិការណ៍នៅលើព្រឹត្តិការណ៍ click។ នៅខាងក្នុងមុខងារ ពិនិត្យធាតុប្រអប់ធីក ស្វែងរកមេរបស់វា (todo div) ហើយប្រើគុណលក្ខណៈទិន្នន័យ todo របស់វា ដើម្បីស្វែងរកការងារត្រូវធ្វើដែលត្រូវគ្នានៅក្នុងអារេទិន្នន័យ។ អ្នកអាចបិទបើកស្ថានភាពដែលបានបញ្ចប់ ធ្វើបច្ចុប្បន្នភាពលក្ខណៈសម្បត្តិរបស់វា និងបង្ហាញ UI ឡើងវិញ៖const html = (todos) => `
<!doctype html>
<html>
<!-- existing content -->
<script>
var populateTodos = function() {
window.todos.forEach(todo => {
// Existing todo element set up code
checkbox.addEventListener("click", completeTodo)
})
}
var completeTodo = function(evt) {
var checkbox = evt.target
var todoElement = checkbox.parentNode
var newTodoSet = [].concat(window.todos)
var todo = newTodoSet.find(t => t.id == todoElement.dataset.todo)
todo.completed = !todo.completed
todos = newTodoSet
updateTodos()
}
</script>
`; todos, cập nhật bộ nhớ cache Cloudflare KV của bạn với giá trị đó, và sau đó thực hiện một render lại của UI dựa trên dữ liệu mà nó có tại địa phương.todos variable, updates your Cloudflare KV cache with that value, and then does a re-render of the UI based on the data it has locally.todos ធ្វើបច្ចុប្បន្នភាពឃ្លាំងសម្ងាត់ Cloudflare KV របស់អ្នកជាមួយនឹងតម្លៃនោះ ហើយបន្ទាប់មកធ្វើការបង្ហាញ UI ឡើងវិញដោយផ្អែកលើទិន្នន័យដែលវាមាននៅក្នុងមូលដ្ឋាន។Sáu Kết luận và các bước tiếp theo 6. Conclusion and next steps 6. សេចក្តីសន្និដ្ឋាន និងជំហានបន្ទាប់
Phần «Conclusion và next steps» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "6. Conclusion and next steps" section below — open the official docs link for full screenshots and configuration tabs.
អានផ្នែក «6. សេចក្តីសន្និដ្ឋាន និងជំហានបន្ទាប់» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។
data – điều này có nghĩa là bất kỳ khách truy cập nào đến trang web sẽ chia sẻ cùng một danh sách todo với những khách truy cập khác. Trong Worker, bạn có thể sử dụng các giá trị từ yêu cầu khách hàng để tạo và duy trì danh sách cụ thể cho người dùng. Ví dụ, bạn có thể tạo một khóa bộ nhớ cache dựa trên yêu cầu IP:data – this means that any visitor to the site will share the same todo list with other visitors. Within your Worker, you could use values from the client request to create and maintain user-specific lists. For example, you may generate a cache key based on the requesting IP:data – នេះមានន័យថាអ្នកចូលមើលគេហទំព័រនឹងចែករំលែកបញ្ជីការងារត្រូវធ្វើដូចគ្នាជាមួយអ្នកទស្សនាផ្សេងទៀត។ នៅក្នុងកម្មកររបស់អ្នក អ្នកអាចប្រើតម្លៃពីសំណើ client ដើម្បីបង្កើត និងរក្សាបញ្ជីអ្នកប្រើប្រាស់ជាក់លាក់។ ឧទាហរណ៍ អ្នកអាចបង្កើតសោឃ្លាំងសម្ងាត់ដោយផ្អែកលើការស្នើសុំ IP៖export default {
async fetch(request, env, ctx) {
const defaultData = {
todos: [
{
id: 1,
name: "Finish the Cloudflare Workers blog post",
completed: false,
},
],
};
const setCache = (key, data) => env.TODOS.put(key, data);
const getCache = (key) => env.TODOS.get(key);
const ip = request.headers.get("CF-Connecting-IP");
const myKey = `data-${ip}`;
if (request.method === "PUT") {
const body = await request.text();
try {
JSON.parse(body);
await setCache(myKey, body);
return new Response(body, { status: 200 });
} catch (err) {
return new Response(err, { status: 500 });
}
}
let data;
const cache = await getCache();
if (!cache) {
await setCache(myKey, JSON.stringify(defaultData));
data = defaultData;
} else {
data = JSON.parse(cache);
}
const body = html(JSON.stringify(data.todos).replace(/</g, "\\u003c"));
return new Response(body, {
headers: {
"Content-Type": "text/html",
},
});
},
}; const html = (todos) => `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Todos</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"></link>
</head>
<body class="bg-blue-100">
<div class="w-full h-full flex content-center justify-center mt-8">
<div class="bg-white shadow-md rounded px-8 pt-6 py-8 mb-4">
<h1 class="block text-grey-800 text-md font-bold mb-2">Todos</h1>
<div class="flex">
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-800 leading-tight focus:outline-none focus:shadow-outline" type="text" name="name" placeholder="A new todo"></input>
<button class="bg-blue-500 hover:bg-blue-800 text-white font-bold ml-2 py-2 px-4 rounded focus:outline-none focus:shadow-outline" id="create" type="submit">Create</button>
</div>
<div class="mt-4" id="todos"></div>
</div>
</div>
</body>
<script>
window.todos = ${todos}
var updateTodos = function() {
fetch("/", { method: "PUT", body: JSON.stringify({ todos: window.todos }) })
populateTodos()
}
var completeTodo = function(evt) {
var checkbox = evt.target
var todoElement = checkbox.parentNode
var newTodoSet = [].concat(window.todos)
var todo = newTodoSet.find(t => t.id == todoElement.dataset.todo)
todo.completed = !todo.completed
window.todos = newTodoSet
updateTodos()
}
var populateTodos = function() {
var todoContainer = document.querySelector("#todos")
todoContainer.innerHTML = null
window.todos.forEach(todo => {
var el = document.createElement("div")
el.className = "border-t py-4"
el.dataset.todo = todo.id
var name = document.createElement("span")
name.className = todo.completed ? "line-through" : ""
name.textContent = todo.name
var checkbox = document.createElement("input")
checkbox.className = "mx-4"
checkbox.type = "checkbox"
checkbox.checked = todo.completed ? 1 : 0
checkbox.addEventListener("click", completeTodo)
el.appendChild(checkbox)
el.appendChild(name)
todoContainer.appendChild(el)
})
}
populateTodos()
var createTodo = function() {
var input = document.querySelector("input[name=name]")
if (input.value.length) {
window.todos = [].concat(todos, { id: window.todos.length + 1, name: input.value, completed: false })
input.value = ""
updateTodos()
}
}
document.querySelector("#create").addEventListener("click", createTodo)
</script>
</html>
`;
export default {
async fetch(request, env, ctx) {
const defaultData = {
todos: [
{
id: 1,
name: "Finish the Cloudflare Workers blog post",
completed: false,
},
],
};
const setCache = (key, data) => env.TODOS.put(key, data);
const getCache = (key) => env.TODOS.get(key);
const ip = request.headers.get("CF-Connecting-IP");
const myKey = `data-${ip}`;
if (request.method === "PUT") {
const body = await request.text();
try {
JSON.parse(body);
await setCache(myKey, body);
return new Response(body, { status: 200 });
} catch (err) {
return new Response(err, { status: 500 });
}
}
let data;
const cache = await getCache();
if (!cache) {
await setCache(myKey, JSON.stringify(defaultData));
data = defaultData;
} else {
data = JSON.parse(cache);
}
const body = html(JSON.stringify(data.todos).replace(/</g, "\\u003c"));
return new Response(body, {
headers: {
"Content-Type": "text/html",
},
});
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/workers/","name":"Workers"}},{"@type":"ListItem","position":3,"item":{"@id":"/workers/tutorials/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/workers/tutorials/build-a-jamstack-app/","name":"Build a todo list Jamstack application"}}]} Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs). មើលមគ្គុទ្ទេសក៍ពេញលើ developers.cloudflare.com (រូបភាព, tab កំណត់)។
Tài liệu gốc ↗ Official docs ↗ ឯកសារផ្លូវការ ↗