[ PROMPT_NODE_24460 ]
Workers Playground 说明文档
[ SKILL_DOCUMENTATION ]
# Cloudflare Workers Playground 技能参考
## 概述
Cloudflare Workers Playground 是一个基于浏览器的沙箱,用于即时实验、测试和部署 Cloudflare Workers,无需身份验证或设置。本技能提供了专门针对 Workers Playground 开发的模式、API 和最佳实践。
**URL:** [workers.cloudflare.com/playground](https://workers.cloudflare.com/playground)
## ⚠️ Playground 限制
**Playground 不等同于生产环境:**
- ✅ 真实的智能体运行时,即时测试,可共享 URL
- ❌ 不支持 TypeScript(仅限 JavaScript)
- ❌ 无绑定(KV、D1、R2、Durable Objects)
- ❌ 无环境变量或密钥
- ❌ 仅限 ES 模块(不支持 Service Worker 格式)
- ⚠️ Safari 浏览器存在兼容问题(请使用 Chrome/Firefox)
**生产环境:** 请使用 `wrangler` CLI。Playground 仅用于快速原型设计。
## 快速入门
最小化智能体:
javascript
export default {
async fetch(request, env, ctx) {
return new Response('Hello World');
}
};
JSON API:
javascript
export default {
async fetch(request, env, ctx) {
const data = { message: 'Hello', timestamp: Date.now() };
return Response.json(data);
}
};
带修改的代理:
javascript
export default {
async fetch(request, env, ctx) {
const response = await fetch('https://example.com');
const modified = new Response(response.body, response);
modified.headers.set('X-Custom-Header', 'added-by-worker');
return modified;
}
};
从 CDN 导入:
javascript
import { Hono } from 'https://esm.sh/hono@3';
export default {
async fetch(request) {
const app = new Hono();
app.get('/', (c) => c.text('Hello Hono!'));
return app.fetch(request);
}
};
## 阅读顺序
1. **[configuration.md](configuration.md)** - 从这里开始:playground 设置、限制、部署
2. **[api.md](api.md)** - 核心 API:Request、Response、ExecutionContext、fetch、Cache
3. **[patterns.md](patterns.md)** - 常见用例:路由、代理、A/B 测试、多模块代码
4. **[gotchas.md](gotchas.md)** - 故障排除:错误、浏览器问题、限制、最佳实践
## 本参考内容
- **[configuration.md](configuration.md)** - 设置、部署、配置
- **[api.md](api.md)** - API 端点、方法、接口
- **[patterns.md](patterns.md)** - 常见模式、用例、示例
- **[gotchas.md](gotchas.md)** - 故障排除、最佳实践、局限性
## 关键特性
**无需设置:**
- 打开 URL 即可开始