Serverless Functions Development for Websites (Cloudflare Workers)
Cloudflare Workers run on V8 isolates — not Node.js and not containers. This provides cold start in less than 1 ms and execution on 300+ edge nodes worldwide. The free tier includes 100,000 requests per day.
Difference from AWS Lambda and Vercel Functions
Workers run on every Cloudflare PoP — a user from Moscow gets an answer from the nearest node, not from us-east-1. This is crucial for latency-sensitive tasks. Limitation: Workers use Web API, not Node.js API — fs, child_process, native modules are unavailable.
Basic Worker
// src/index.ts
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/contact" && request.method === "POST") {
return handleContact(request, env);
}
if (url.pathname === "/api/geo") {
return handleGeo(request);
}
return new Response("Not found", { status: 404 });
},
};
async function handleContact(request: Request, env: Env): Promise<Response> {
const data = await request.json<{ name: string; email: string; message: string }>();
// Send via Resend API
const emailResponse = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
Authorization: `Bearer ${env.RESEND_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
from: "[email protected]",
to: ["[email protected]"],
subject: `Message from ${data.name}`,
text: `${data.name} (${data.email}): ${data.message}`,
}),
});
if (!emailResponse.ok) {
return Response.json({ error: "Email send failed" }, { status: 500 });
}
return Response.json({ ok: true });
}
// Geolocation from Cloudflare headers
function handleGeo(request: Request): Response {
const cf = (request as any).cf;
return Response.json({
country: cf?.country,
city: cf?.city,
timezone: cf?.timezone,
latitude: cf?.latitude,
longitude: cf?.longitude,
});
}
Wrangler and Deployment
# wrangler.toml
name = "my-site-api"
main = "src/index.ts"
compatibility_date = "2024-11-01"
[vars]
ENVIRONMENT = "production"
[[routes]]
pattern = "yourdomain.com/api/*"
zone_name = "yourdomain.com"
npm install -g wrangler
wrangler login
wrangler dev # local development
wrangler deploy # deployment
Secrets:
wrangler secret put RESEND_API_KEY
wrangler secret put DATABASE_URL
Workers KV: Data Storage
KV is a key-value store, eventually consistent. Good for caching, sessions, configuration.
// Binding in wrangler.toml
// [[kv_namespaces]]
// binding = "CACHE"
// id = "abc123..."
export default {
async fetch(request: Request, env: Env & { CACHE: KVNamespace }) {
const cacheKey = new URL(request.url).pathname;
const cached = await env.CACHE.get(cacheKey);
if (cached) {
return new Response(cached, {
headers: { "Content-Type": "application/json", "X-Cache": "HIT" }
});
}
const data = await fetchFreshData(request);
await env.CACHE.put(cacheKey, JSON.stringify(data), { expirationTtl: 300 });
return Response.json(data);
}
};
D1: SQLite on Edge
D1 is Cloudflare's serverless SQLite database. Good for small datasets (up to 10 GB).
export default {
async fetch(request: Request, env: Env & { DB: D1Database }) {
const { results } = await env.DB.prepare(
"SELECT * FROM products WHERE category = ? ORDER BY created_at DESC LIMIT 20"
).bind("electronics").all();
return Response.json(results);
}
};
Workers Limitations
- CPU time: 10 ms (free), 30 s (Paid)
- Memory: 128 MB
- No Node.js built-ins (
fs,path,crypto— only Web Crypto API) - No long-lived connections to PostgreSQL (use Hyperdrive or HTTP API)
Hyperdrive: PostgreSQL from Workers
export default {
async fetch(request: Request, env: Env & { HYPERDRIVE: Hyperdrive }) {
const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
await client.connect();
const result = await client.query("SELECT * FROM users WHERE id = $1", [userId]);
await client.end();
return Response.json(result.rows[0]);
}
};
Hyperdrive proxies connections to external PostgreSQL through a connection pool on Cloudflare's side, solving the latency problem when connecting to a remote database.
Timeframe
Basic Worker with routing and 3–5 endpoints — 2–3 days. KV and D1 integration, CI/CD via GitHub Actions — plus 2 days.







