Loading...
Loading...
Build serverless applications on Cloudflare Workers. Covers runtime APIs, handlers (fetch, scheduled, queue, email), bindings (KV, R2, D1, Durable Objects, Queues, AI, Vectorize), wrangler.toml configuration, local development with Miniflare, static assets, compatibility flags, testing with Vitest. Keywords: Cloudflare Workers, serverless, edge computing, Wrangler, fetch handler, scheduled handler, bindings, KV, R2, D1, Durable Objects, Workers AI, Miniflare, wrangler.toml, compatibility_date.
npx skill4agent add itechmeat/llm-code cloudflare-workersreferences/runtime.mdreferences/bindings.mdreferences/wrangler.mdreferences/local-dev.mdreferences/assets.mdreferences/testing.mdexport default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return new Response("Hello, World!");
},
} satisfies ExportedHandler<Env>;| Handler | Trigger | Use Case |
|---|---|---|
| HTTP request | APIs, websites |
| Cron trigger | Background jobs |
| Queue message | Async processing |
| Email received | Email routing |
env| Binding | Type | Local Dev |
|---|---|---|
| KV | | ✅ Simulated |
| R2 | | ✅ Simulated |
| D1 | | ✅ Simulated |
| Durable Objects | | ✅ Simulated |
| Queues | | ✅ Simulated |
| Workers AI | | ❌ Remote only |
| Vectorize | | ❌ Remote only |
| Browser Rendering | | ❌ Remote only |
| Hyperdrive | | ❌ Remote only |
| Static Assets | | ✅ Simulated |
| Service Binding | | ✅ Simulated |
// wrangler.jsonc
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2025-03-07",
"compatibility_flags": ["nodejs_compat"],
"observability": {
"enabled": true,
"head_sampling_rate": 1
}
}# Development
npx wrangler dev # Local dev (Miniflare)
npx wrangler dev --remote # Remote dev (code on Cloudflare)
# Deployment
npx wrangler deploy # Deploy to production
npx wrangler versions upload # Upload without deploying
npx wrangler versions deploy # Promote version
# Secrets
npx wrangler secret put KEY # Add secret
npx wrangler secret list # List secrets
# Types
npx wrangler types # Generate TypeScript types# wrangler.toml
compatibility_date = "2025-03-07"
compatibility_flags = ["nodejs_compat"]nodejs_compatnodejs_compat_v2export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/hello") {
return Response.json({ message: "Hello!" });
}
if (url.pathname.startsWith("/static/")) {
return env.ASSETS.fetch(request);
}
return new Response("Not Found", { status: 404 });
},
};export default {
async scheduled(controller: ScheduledController, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(doBackgroundWork(env));
},
};# wrangler.toml
[triggers]
crons = ["0 * * * *"] # Every hourexport default {
async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext) {
for (const message of batch.messages) {
console.log(message.body);
message.ack();
}
},
};wrangler.tomlwrangler secret putremote: truecompatibility_date| Issue | Solution |
|---|---|
| Local bindings empty | Add |
| AI not working locally | Use |
| Node APIs unavailable | Add |
| First deploy fails | Use |
cloudflare-pagescloudflare-d1cloudflare-r2cloudflare-kvcloudflare-durable-objects