您将向 Worker 发送包含 JavaScript 的 POST 请求,并读取 {"result":["Ada"]}。
- 注册 Cloudflare 账户 ↗。
- 安装
Node.js↗。
-
创建一个 Worker 项目:
npm create cloudflare@latest -- sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deployyarn create cloudflare sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deploypnpm create cloudflare@latest sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deploy -
进入项目目录:
cd sandbox-dynamic-worker -
替换
wrangler.jsonc,添加 Worker Loader 绑定:{ "$schema": "node_modules/wrangler/config-schema.json", "name": "sandbox-dynamic-worker", "main": "src/index.ts", // Set this to today's date "compatibility_date": "2026-11-14", "observability": { "enabled": true, }, "upload_source_maps": true, "worker_loaders": [ { "binding": "LOADER", }, ], }"$schema" = "node_modules/wrangler/config-schema.json" name = "sandbox-dynamic-worker" main = "src/index.ts" # Set this to today's date compatibility_date = "2026-11-14" upload_source_maps = true [observability] enabled = true [[worker_loaders]] binding = "LOADER" -
为绑定生成类型:
npx wrangler typesyarn wrangler typespnpm wrangler types -
替换
src/index.ts。您的 Worker 会读取 JSON 正文中的code,并在沙箱中运行它:src/index.jsjs export default { async fetch(request, env) { const { code } = await request.json(); const sandbox = env.LOADER.load({ compatibilityDate: "2026-11-14", mainModule: "code.js", modules: { "code.js": ` import { WorkerEntrypoint } from "cloudflare:workers"; export class Code extends WorkerEntrypoint { evaluate() { ${code} } } `, }, // Block `fetch()` and `connect()` globalOutbound: null, // Stop code that uses more than 50 milliseconds of CPU time limits: { cpuMs: 50 }, }); const result = await sandbox.getEntrypoint("Code").evaluate(); return Response.json({ result }); }, };src/index.tsts import type { WorkerEntrypoint } from "cloudflare:workers"; type CodeEntrypoint = WorkerEntrypoint & { evaluate(): Promise<unknown>; }; export default { async fetch(request: Request, env: Env): Promise<Response> { const { code } = (await request.json()) as { code: string }; const sandbox = env.LOADER.load({ compatibilityDate: "2026-11-14", mainModule: "code.js", modules: { "code.js": ` import { WorkerEntrypoint } from "cloudflare:workers"; export class Code extends WorkerEntrypoint { evaluate() { ${code} } } `, }, // Block `fetch()` and `connect()` globalOutbound: null, // Stop code that uses more than 50 milliseconds of CPU time limits: { cpuMs: 50 }, }); const result = await sandbox .getEntrypoint<CodeEntrypoint>("Code") .evaluate(); return Response.json({ result }); }, };code是evaluate()的函数体,因此需要包含return语句。不占用 CPU 的等待代码仍可能让请求保持打开状态,所以在运行他人提供的代码前,应先设置超时。有关示例,请参阅构建 AI 代码解释器。 -
运行
wrangler dev:npx wrangler devyarn wrangler devpnpm wrangler dev -
向 Wrangler 输出的 URL 发送包含 JavaScript 的 POST 请求。默认 URL 为
http://localhost:8787:curl http://localhost:8787 --request POST --json '{ "code": "const users = [{ name: \"Ada\", role: \"admin\" }, { name: \"Grace\", role: \"user\" }]; return users.filter((u) => u.role === \"admin\").map((u) => u.name);" }'
响应正文为 {"result":["Ada"]}。Worker 已运行您发送的 JavaScript。
- 允许代码调用您的 Worker 提供的方法。请参阅绑定。
- 运行 Linux 命令。请参阅运行 Linux 命令。
- 使用 Loader API 中的
load()和get()。