التعامل مع طلبات HTTP
اكتبوا Worker أساسيًا لاعتراض طلبات HTTP والاستجابة لها، مع توضيح الوظائف الأساسية.
التعامل مع طلبات HTTP درس مجاني في Edge Computing with Cloudflare Workers & Deno على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Edge Computing with Cloudflare Workers & Deno، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Edge Computing with Cloudflare Workers & Deno 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Workers and Web Requests
Cloudflare Workers are powerful tools that sit at the edge of the internet. Think of them as highly efficient gatekeepers for your web traffic.
Their primary job is to intercept incoming HTTP requests, process them using your code, and then send back an appropriate HTTP response.
Understanding the Request
Every time a user's browser interacts with your Worker, it receives a Request object. This object is packed with all the details about the incoming request:
- URL: The full web address the user tried to reach.
- Method: The HTTP action, like GET (fetching data) or POST (sending data).
- Headers: Extra information such as the browser type, cookies, and preferred language.
- Body: Any data sent with the request, common with POST or PUT methods.
Catching the Fetch Event
Cloudflare Workers use an event listener to 'catch' incoming requests. The fetch event is triggered for every HTTP request that hits your Worker.
The standard entry point for a Worker is an exported object with an async fetch method. This method receives the request object as its first argument.
export default {
async fetch(request, env, ctx) {
// Your Worker logic goes here
// The 'request' object holds all incoming request details
return new Response("Hello from Worker!");
}
}Crafting a Simple Response
After your Worker processes a request, it needs to send back a Response object. This object contains the content and metadata that the user's browser will receive.
A basic Response can be created by simply passing a string of text to its constructor.
export default {
async fetch(request, env, ctx) {
const responseText = "Welcome to the edge!";
return new Response(responseText);
}
}Your First 'Hello Worker!'
Let's combine what we've learned to create a simple Worker that says "Hello Cloudflare Worker!" to every incoming request.
This is the fundamental structure you'll use for almost all Cloudflare Workers.
export default {
async fetch(request, env, ctx) {
return new Response("Hello Cloudflare Worker!");
}
}Peeking at the Request URL
You can access various properties of the request object to get information about the incoming request. For example, request.url gives you the full URL that was requested.
This allows you to create dynamic responses based on the specific path or query parameters a user is trying to reach.
export default {
async fetch(request, env, ctx) {
const url = request.url;
return new Response(`You requested: ${url}`);
}
}Responding to GET vs. POST
The request.method property tells you the HTTP method used (e.g., GET, POST, PUT, DELETE). You can use this to provide different logic for different types of requests.
For instance, a GET request might fetch data, while a POST request might submit data for creation.
export default {
async fetch(request, env, ctx) {
if (request.method === "GET") {
return new Response("This is a GET request.");
} else if (request.method === "POST") {
return new Response("This is a POST request.", { status: 201 });
}
return new Response("Method not allowed.", { status: 405 });
}
}Custom Headers and Status
You can customize the Response object further by adding HTTP headers or changing the status code. This is done by passing an options object as the second argument to the Response constructor.
- Headers: Provide metadata about the response (e.g.,
Content-Type: text/plain). - Status Code: Indicates the outcome of the request (e.g.,
200 OK,404 Not Found).
export default {
async fetch(request, env, ctx) {
return new Response("Custom response!", {
status: 200,
headers: {
"Content-Type": "text/plain",
"X-Worker-Info": "Processed at the Edge"
}
});
}
}Fetching from an Origin
Often, your Worker will act as a proxy. This means it fetches content from an 'origin server' (your actual website or API backend) and potentially modifies it before sending it to the user.
You can use the global fetch() function, passing it the original request object, to forward the request to another server.
export default {
async fetch(request, env, ctx) {
// Fetch the request from the origin server
const response = await fetch(request);
// You can modify the response here before returning it
return response;
}
}Worker Response Check
You're building a Cloudflare Worker. Which of the following correctly sets the status code of a response to 404 Not Found?
Recap: Handling HTTP Requests
In this lesson, you learned the core of Cloudflare Workers: how they handle HTTP requests and send back responses.
- Workers intercept
fetchevents. - The
Requestobject contains all incoming request data. - You return a
Responseobject to the user. - You can access request details like URL and method.
- Responses can have custom status codes and headers.
- Workers can act as proxies, fetching content from origin servers.
Next, you'll learn the steps to deploy your first Worker to Cloudflare's edge network so it can start serving users globally!
الأسئلة الشائعة
هل درس «التعامل مع طلبات HTTP» مجاني؟
نعم — نص درس «التعامل مع طلبات HTTP» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Edge Computing with Cloudflare Workers & Deno، انتقل إلى CoddyKit PRO. تتضمن دورة Edge Computing with Cloudflare Workers & Deno 4 دروس في المجموع.
ماذا ستتعلم في «التعامل مع طلبات HTTP»؟
اكتبوا Worker أساسيًا لاعتراض طلبات HTTP والاستجابة لها، مع توضيح الوظائف الأساسية. تتمرن على Edge Computing with Cloudflare Workers & Deno مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Edge Computing with Cloudflare Workers & Deno؟
لا تُشترط خبرة سابقة. Edge Computing with Cloudflare Workers & Deno على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التعامل مع طلبات HTTP»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Edge Computing with Cloudflare Workers & Deno هذا؟
نعم. كل درس في Edge Computing with Cloudflare Workers & Deno يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد بيئة Worker
- التعامل مع طلبات HTTP
- نشر أول Worker لكم
- متغيرات البيئة والأسرار في Workers