编程

cloudflare-workers

试用

用 Wrangler CLI 在 Cloudflare 全球边缘网络上开发并部署 JavaScript、TypeScript、Python 或 Rust 代码。

它能做什么

Cloudflare Workers 是无服务器运行时,在 V8 isolate 中执行 JS、TS、Python 或 Rust 代码,部署在 300+ 城市节点,冷启动接近为零。开发流程全部围绕 Wrangler CLI:`npm create cloudflare` 脚手架生成项目,`wrangler dev` 本地热更新调试,`wrangler deploy` 一键发布。在 `wrangler.toml` 中声明的 bindings 可直接对接 D1(SQL)、KV(键值)、R2(对象存储)、Durable Objects、Queues、Workflows、Containers、Workers AI 等资源。处理器支持 fetch、scheduled、queue 三种事件。框架层面适配 Hono、Next.js、Remix、Astro、SvelteKit 与 tRPC,测试可借助 cloudflare:test 与 Vitest 集成。

什么时候用它

  • 构建 REST、GraphQL 或 tRPC 风格的 HTTP API
  • 用 cron 定时器或 Queue 消费者处理后台任务与异步消息
  • 通过 ASSETS 绑定同时托管前后端代码与静态资源
  • 对接 Workers AI 与 Vectorize 做聊天机器人或 RAG 流程

技能文档

Cloudflare Workers

Overview

Cloudflare Workers is a serverless execution environment that runs JavaScript, TypeScript, Python, and Rust code on Cloudflare's global network. Workers execute in milliseconds, scale automatically, and integrate with Cloudflare's storage and compute products through bindings.

Key Benefits:

  • Zero cold starts - Workers run in V8 isolates, not containers
  • Global deployment - Code runs in 300+ cities worldwide
  • Rich ecosystem - Bindings to D1, KV, R2, Durable Objects, Queues, Containers, Workflows, and more
  • Full-stack capable - Build APIs and serve static assets in one project
  • Standards-based - Uses Web APIs (fetch, crypto, streams, WebSockets)

When to Use This Skill

Use Cloudflare Workers for:

  • APIs and backends - RESTful APIs, GraphQL, tRPC, WebSocket servers
  • Full-stack applications - React, Next.js, Remix, Astro, Vue, Svelte with static assets
  • Edge middleware - Authentication, rate limiting, A/B testing, routing
  • Background processing - Scheduled jobs (cron), queue consumers, webhooks
  • Data transformation - ETL pipelines, real-time data processing
  • AI applications - RAG systems, chatbots, image generation with Workers AI
  • Durable workflows - Multi-step long-running tasks with automatic retries (Workflows)
  • Container workloads - Run Docker containers alongside Workers (Containers)
  • MCP servers - Host remote Model Context Protocol servers
  • Proxy and gateway - API gateways, content transformation, protocol translation

Quick Start Workflow

1. Install Wrangler CLI

npm install -g wrangler

# Login to Cloudflare
wrangler login

2. Create a New Worker

# Using C3 (create-cloudflare) - recommended
npm create cloudflare@latest my-worker

# Or create manually
wrangler init my-worker
cd my-worker

3. Write Your Worker

Basic HTTP API (TypeScript):

export default {
  async fetch(request: Request, env: Env): Promise {
    const url = new URL(request.url);

    if (url.pathname === "/api/hello") {
      return Response.json({ message: "Hello from Workers!" });
    }

    return new Response("Not found", { status: 404 });
  },
};

With environment variables and KV:

interface Env {
  MY_VAR: string;
  MY_KV: KVNamespace;
}

export default {
  async fetch(request: Request, env: Env): Promise {
    // Access environment variable
    const greeting = env.MY_VAR;

    // Read from KV
    const value = await env.MY_KV.get("my-key");

    return Response.json({ greeting, value });
  },
};

4. Develop Locally

# Start local development server with hot reload
wrangler dev

# Access at http://localhost:8787

5. Deploy to Production

# Deploy to workers.dev subdomain
wrangler deploy

# Deploy to custom domain (configure in wrangler.toml)
wrangler deploy

Core Concepts

Workers Runtime

Workers use the V8 JavaScript engine with Web Standard APIs:

  • Execution model: Isolates (not containers) - instant cold starts
  • CPU time limit: 10ms (Free), 30s (Paid) per request
  • Memory limit: 128 MB per isolate
  • Languages: JavaScript, TypeScript, Python, Rust
  • APIs: fetch, crypto, streams, WebSockets, WebAssembly

Supported APIs:

  • Fetch API (HTTP requests)
  • URL API (URL parsing)
  • Web Crypto (encryption, hashing)
  • Streams API (data streaming)
  • WebSockets (real-time communication)
  • Cache API (edge caching)
  • HTML Rewriter (HTML transformation)

Handlers

Workers respond to events through handlers:

Fetch Handler (HTTP requests):

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    return new Response("Hello!");
  },
};

Scheduled Handler (cron jobs):

export default {
  async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
    // Runs on schedule defined in wrangler.toml
    await env.MY_KV.put("last-run", new Date().toISOString());
  },
};

Queue Handler (message processing):

export default {
  async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext) {
    for (const message of batch.messages) {
      await processMessage(message.body);
      message.ack();
    }
  },
};

Bindings

Bindings connect your Worker to Cloudflare resources. Configure in wrangler.toml:

KV (Key-Value Storage):

[[kv_namespaces]]
binding = "MY_KV"
id = "your-kv-namespace-id"
// Usage
await env.MY_KV.put("key", "value");
const value = await env.MY_KV.get("key");

D1 (SQL Database):

[[d1_databases]]
binding = "DB"
database_name = "my-database"
database_id = "your-database-id"
// Usage
const result = await env.DB.prepare(
  "SELECT * FROM users WHERE id = ?"
).bind(userId).all();

R2 (Object Storage):

[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "my-bucket"
// Usage
await env.MY_BUCKET.put("file.txt", "contents");
const object = await env.MY_BUCKET.get("file.txt");
const text = await object?.text();

Environment Variables (non-secret only):

[vars]
GREETING = "hello"
ENVIRONMENT = "development"

Secrets (sensitive data — never put in wrangler.toml):

# Set via CLI; the value is encrypted server-side and never written to disk locally
wrangler secret put MY_API_KEY

Context (ctx)

The ctx parameter provides control over request lifecycle:

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    // Run tasks after response is sent
    ctx.waitUntil(
      env.MY_KV.put("request-count", String(Date.now()))
    );

    // Pass through to origin on exception
    ctx.passThroughOnException();

    return new Response("OK");
  },
};

Top-level Environment Access

Since March 2025, you can import env at the module level instead of passing it through handlers:

import { env } from "cloudflare:workers";

// Access bindings outside of handlers
const apiClient = new ApiClient({ apiKey: env.API_KEY });

export default {
  async fetch(request: Request): Promise {
    // env is also available here without the parameter
    const data = await env.MY_KV.get("config");
    return Response.json({ data });
  },
};

This eliminates prop-drilling env through function signatures and enables module-level initialization.

Rapid Development Patterns

Wrangler Configuration

Essential wrangler.toml:

name = "my-worker"
main = "src/index.ts"
compatibility_date = "2025-09-01"

# Custom domain
routes = [
  { pattern = "api.example.com/*", zone_name = "example.com" }
]

# Or workers.dev subdomain
workers_dev = true

# Environment variables
[vars]
ENVIRONMENT = "production"

# Bindings
[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-id"

[[d1_databases]]
binding = "DB"
database_name = "production-db"
database_id = "your-db-id"

[[r2_buckets]]
binding = "ASSETS"
bucket_name = "my-assets"

# Cron triggers
[triggers]
crons = ["0 0 * * *"]  # Daily at midnight

Environment Management

Use environments for staging/production:

[env.staging]
vars = { ENVIRONMENT = "staging" }

[env.staging.d1_databases]
binding = "DB"
database_name = "staging-db"
database_id = "staging-db-id"

[env.production]
vars = { ENVIRONMENT = "production" }

[env.production.d1_databases]
binding = "DB"
database_name = "production-db"
database_id = "production-db-id"
# Deploy to staging
wrangler deploy --env staging

# Deploy to production
wrangler deploy --env production

Common Patterns

JSON API with Error Handling:

export default {
  async fetch(request: Request, env: Env): Promise {
    try {
      const url = new URL(request.url);

      if (url.pathname === "/api/users" && request.method === "GET") {
        const users = await env.DB.prepare("SELECT * FROM users").all();
        return Response.json(users.results);
      }

      if (url.pathname === "/api/users" && request.method === "POST") {
        const body = await request.json();
        await env.DB.prepare(
          "INSERT INTO users (name, email) VALUES (?, ?)"
        ).bind(body.name, body.email).run();
        return Response.json({ success: true }, { status: 201 });
      }

      return Response.json({ error: "Not found" }, { status: 404 });
    } catch (error) {
      return Response.json(
        { error: error.message },
        { status: 500 }
      );
    }
  },
};

Authentication Middleware:

async function authenticate(request: Request, env: Env): Promise {
  const authHeader = request.headers.get("Authorization");
  if (!authHeader?.startsWith("Bearer ")) {
    return null;
  }

  const token = authHeader.substring(7);
  const userId = await env.SESSIONS.get(token);
  return userId;
}

export default {
  async fetch(request: Request, env: Env): Promise {
    const userId = await authenticate(request, env);

    if (!userId) {
      return Response.json({ error: "Unauthorized" }, { status: 401 });
    }

    // Proceed with authenticated request
    return Response.json({ userId });
  },
};

CORS Headers:

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
  "Access-Control-Allow-Headers": "Content-Type, Authorization",
};

export default {
  async fetch(request: Request): Promise {
    if (request.method === "OPTIONS") {
      return new Response(null, { headers: corsHeaders });
    }

    const response = await handleRequest(request);

    // Add CORS headers to response
    Object.entries(corsHeaders).forEach(([key, value]) => {
      response.headers.set(key, value);
    });

    return response;
  },
};

Static Assets (Full-Stack Apps)

Serve static files alongside your Worker code:

[assets]
directory = "./public"
binding = "ASSETS"
export default {
  async fetch(request: Request, env: Env): Promise {
    const url = new URL(request.url);

    // API routes
    if (url.pathname.startsWith("/api/")) {
      return handleAPI(request, env);
    }

    // Serve static assets via the ASSETS binding
    return env.ASSETS.fetch(request);
  },
};

Testing

Using Vitest:

import { env, createExecutionContext } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import worker from "./index";

describe("Worker", () => {
  it("responds with JSON", async () => {
    const request = new Request("http://example.com/api/hello");
    const ctx = createExecutionContext();
    const response = await worker.fetch(request, env, ctx);

    expect(response.status).toBe(200);
    expect(await response.json()).toEqual({ message: "Hello!" });
  });
});

Framework Integration

Workers supports major frameworks with adapters:

  • Next.js - Full App Router and Pages Router support
  • Remix / React Router - Native Cloudflare adapter
  • Astro - Server-side rendering on Workers
  • SvelteKit - Cloudflare adapter available
  • Hono - Lightweight web framework built for Workers
  • tRPC - Type-safe APIs with full Workers support

Example with Hono:

import { Hono } from "hono";

const app = new Hono();

app.get("/", (c) => c.text("Hello!"));
app.get("/api/users/:id", async (c) => {
  const id = c.req.param("id");
  const user = await c.env.DB.prepare(
    "SELECT * FROM users WHERE id = ?"
  ).bind(id).first();
  return c.json(user);
});

export default app;

Advanced Topics

For detailed information on advanced features, see the reference files:

  • Complete Bindings Guide: references/bindings-complete-guide.md - All binding types (D1, KV, R2, Durable Objects, Queues, Workers AI, Vectorize, Workflows, Containers, Secrets Store, Pipelines, AutoRAG)
  • Deployment & CI/CD: references/wrangler-and-deployment.md - Wrangler v4 migration, commands, GitHub Actions, GitLab CI/CD, gradual rollouts, remote bindings
  • Development Best Practices: references/development-patterns.md - Testing, debugging, error handling, performance, top-level env access patterns
  • Advanced Features: references/advanced-features.md - Containers, Workflows, MCP servers, Workers for Platforms, WebSockets, Node.js compat, streaming
  • Observability: references/observability.md - Logging (tail, Logpush, Workers Logs), metrics, traces, debugging

Resources

Official Documentation:

Templates & Quick Starts:

Community:

常见问题

Worker 真的没有冷启动吗?
文档明确指出 Worker 运行在 V8 isolate 而非容器中,因此请求到来时无需启动容器,冷启动为零。
D1、KV 这类绑定在哪里配置?
在 `wrangler.toml` 中声明,例如 `[[d1_databases]]` 与 `[[kv_namespaces]]` 块,绑定会作为字段挂在处理器入口的 `env` 对象上直接使用。
支持哪些前端框架直接部署?
文档列出 Hono、Next.js、Remix、Astro、SvelteKit、tRPC 都提供 Cloudflare 适配器,可以直接构建并部署全栈应用。

相关技能

Cloudflare Worker (workers.cloudflare.com). Use this skill for ANY Cloudflare Worker request — reading, creating, updating, and deleting data. Whenever a task involves Cloudflare Worker, use this skill instead of calling the API directly.

8 次安装

Deploy small, non-sensitive static sites to temporary Cloudflare Workers previews. Use when an intended user explicitly accepts Cloudflare terms and needs a short-lived workers.dev URL with a privately delivered Claim URL.

1 次安装

Deploy applications and infrastructure to Cloudflare using Workers, Pages, and related platform services. Use when the user asks to deploy, host, publish, or...

9 次安装

An agent skill that scaffolds a new, production-oriented full-stack app on the latest version of Nuxt, deployed to Cloudflare Workers via Wrangler — from natural language descriptions like "build me a blog on Cloudflare".

Cloudflare Docs (developers.cloudflare.com). Use this skill for ANY Cloudflare Docs request — searching and reading data. Whenever a task involves Cloudflare Docs, use this skill instead of calling the API directly.

1 次安装

Cloudflare Browser Run (developers.cloudflare.com). Use this skill for ANY Cloudflare Browser Run request — searching and reading data. Whenever a task involves Cloudflare Browser Run, use this skill instead of calling the API directly.

1 次安装