Workers 本地调试
启动
bash
npm run dev:wrangler访问:
text
http://localhost:8787/api/demo如果提示未登录
使用环境变量登录:
bash
npx wrangler login如果你的 Cloudflare 令牌已经配置好,也可以直接运行:
bash
npx wrangler whoami本地 R2 调试
本地调试时,wrangler 会尝试使用你配置的 Cloudflare 凭证访问真实 R2 Bucket。
如果你只想测试函数逻辑,可以先不读写 R2,只返回:
ts
return Response.json({ message: 'ok' });常见错误
binding DEMO_BUCKET not found
检查 wrangler.toml 是否有:
toml
[[r2_buckets]]
binding = "DEMO_BUCKET"
bucket_name = "cloudflare-learning-lab"The bucket does not exist
先创建 Bucket:
bash
npx wrangler r2 bucket create cloudflare-learning-lab网络慢或失败
中国大陆访问 Cloudflare API 有时不稳定。可以先:
- 使用稳定的网络环境。
- 检查代理。
- 使用
npx wrangler --help确认 wrangler 可运行。 - 把错误信息记录下来,再根据具体错误排查。