Configuración del entorno de Workers
Configure su equipo local para desarrollar con Cloudflare Workers mediante Wrangler CLI.
Configuración del entorno de Workers es una lección gratuita de Edge Computing with Cloudflare Workers & Deno en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Edge Computing with Cloudflare Workers & Deno, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Edge Computing with Cloudflare Workers & Deno incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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!
Preguntas frecuentes
¿La lección «Configuración del entorno de Workers» es gratis?
Sí — el texto completo de «Configuración del entorno de Workers» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Edge Computing with Cloudflare Workers & Deno, actualiza a CoddyKit PRO. El curso de Edge Computing with Cloudflare Workers & Deno incluye 4 lecciones en total.
¿Qué aprenderé en «Configuración del entorno de Workers»?
Configure su equipo local para desarrollar con Cloudflare Workers mediante Wrangler CLI. Practicas Edge Computing with Cloudflare Workers & Deno con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Edge Computing with Cloudflare Workers & Deno?
No se requiere experiencia previa. Edge Computing with Cloudflare Workers & Deno en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Configuración del entorno de Workers»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Edge Computing with Cloudflare Workers & Deno?
Sí. Cada lección de Edge Computing with Cloudflare Workers & Deno incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Configuración del entorno de Workers
- Gestión de solicitudes HTTP
- Implementación de su primer Worker
- Variables de entorno y secretos en Workers