إعداد بيئة Worker
اضبطوا جهازكم المحلي لتطوير Cloudflare Workers باستخدام Wrangler CLI.
إعداد بيئة Worker درس مجاني في Edge Computing with Cloudflare Workers & Deno على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Edge Computing with Cloudflare Workers & Deno، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Edge Computing with Cloudflare Workers & Deno 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Get Ready for Edge Dev
Welcome! Before we build our first Cloudflare Worker, we need to set up our local development environment. This ensures you have all the tools to write, test, and deploy your code efficiently.
A proper setup makes the development process smooth and helps you quickly iterate on your edge applications.
Wrangler: Your Worker Tool
Our primary tool for interacting with Cloudflare Workers is the Wrangler CLI (Command Line Interface). It's a powerful tool that helps you:
- Create new Worker projects
- Develop and test Workers locally
- Configure and deploy Workers to Cloudflare's edge
- Manage Worker resources
Essential: Node.js & npm
Wrangler is built using Node.js, so you'll need Node.js and its package manager, npm (or yarn), installed on your machine.
- Node.js: A JavaScript runtime environment.
- npm: Node Package Manager, used to install libraries and tools.
If you don't have Node.js, download it from nodejs.org. We recommend using a Long Term Support (LTS) version.
Installing Wrangler CLI
Once Node.js and npm are ready, open your terminal or command prompt. We'll install Wrangler globally so it's accessible from any directory.
Run the following command:
npm install -g wranglerConnect to Cloudflare
After installing, you need to link Wrangler to your Cloudflare account. This allows Wrangler to deploy and manage Workers on your behalf.
Execute this command and follow the browser prompts to log in:
wrangler loginCreate Your First Project
Now that Wrangler is installed and authenticated, let's create a new Worker project. This command generates a basic project structure for you.
Replace my-first-worker with your desired project name:
wrangler generate my-first-workerUnderstanding Project Structure
After running wrangler generate, you'll find a new directory (e.g., my-first-worker) with essential files:
wrangler.toml: Your Worker's configuration file.src/index.js: The main JavaScript file where your Worker's logic lives.package.json: Standard Node.js package file (optional, for dependencies).
Let's dive into these key files.
Configure with wrangler.toml
The wrangler.toml file is crucial. It defines your Worker's settings, like its name, type, and associated Cloudflare account.
Here's a basic example of what it might contain:
name = "my-first-worker"
main = "src/index.js"
compatibility_date = "2023-10-26"
account_id = "YOUR_CLOUDFLARE_ACCOUNT_ID"Your Worker's Logic
The src/index.js file is where you write the JavaScript code for your Worker. Cloudflare Workers typically listen for fetch events.
This simple Worker responds with "Hello Cloudflare Workers!" to any request:
export default {
async fetch(request, env, ctx) {
return new Response("Hello Cloudflare Workers!");
},
};Test Your Worker Locally
Before deploying, you can test your Worker right on your machine! Wrangler creates a local development server that simulates the Cloudflare edge environment.
Navigate into your project directory (e.g., cd my-first-worker) and run:
wrangler devWrangler Command Check
Which Wrangler command is used to connect your local environment to your Cloudflare account?
Recap: Setup Complete!
Great job! You've successfully set up your Cloudflare Workers development environment. You've learned how to:
- Install Wrangler CLI
- Authenticate with Cloudflare
- Generate a new Worker project
- Understand the basic project structure
- Run your Worker locally using
wrangler dev
You're now ready to start building powerful applications at the edge!
الأسئلة الشائعة
هل درس «إعداد بيئة Worker» مجاني؟
نعم — نص درس «إعداد بيئة Worker» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Edge Computing with Cloudflare Workers & Deno، انتقل إلى CoddyKit PRO. تتضمن دورة Edge Computing with Cloudflare Workers & Deno 4 دروس في المجموع.
ماذا ستتعلم في «إعداد بيئة Worker»؟
اضبطوا جهازكم المحلي لتطوير Cloudflare Workers باستخدام Wrangler CLI. تتمرن على Edge Computing with Cloudflare Workers & Deno مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Edge Computing with Cloudflare Workers & Deno؟
لا تُشترط خبرة سابقة. Edge Computing with Cloudflare Workers & Deno على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إعداد بيئة Worker»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Edge Computing with Cloudflare Workers & Deno هذا؟
نعم. كل درس في Edge Computing with Cloudflare Workers & Deno يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.