Cloudflare Workers fundamentals: module exports, request handling, environment bindings, Wrangler config, multi-environment setup. Use when building serverless edge functions on Cloudflare.
export interface Env {
AI: Ai;
FEED_CACHE: KVNamespace;
MY_DURABLE_OBJECT: DurableObjectNamespace;
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
switch (url.pathname) {
case '/health':
return new Response('OK');
case '/api/summarize':
return handleSummarize(request, env);
default:
return new Response('Not Found', { status: 404 });
}
}
};
fetch handlerEnv interface — AI, KV, Durable Objects, secretswrangler.toml for bindings, routes, environments[env.staging] / [env.production] blocks in wrangler.tomlctx.waitUntil() for work that shouldn't block the responsectx.waitUntil() for background tasksFor detailed patterns, see references/workers.md.