xiaohongshu
xiaohongshu
This skill should be used when the user asks about "Workers API", "fetch handler", "Workers runtime", "request handling", "response handling", "Workers bindings", "environment variables in Workers", "Workers context", or discusses implementing Workers code, routing patterns, or using Cloudflare b...
Full skill instructions
This skill provides comprehensive guidance for developing Cloudflare Workers, including runtime APIs, fetch event handlers, request/response handling, bindings usage, and common development patterns. Use this skill when implementing Workers code, designing Workers architecture, or working with the Workers runtime environment.
Cloudflare Workers run on the V8 JavaScript engine at the edge. Workers use the Service Worker API with extensions specific to the Cloudflare platform.
Workers execute on incoming requests:
The fetch event handler is the entry point for Workers:
export default {
async fetch(request, env, ctx) {
// request: Request object
// env: Bindings and environment variables
// ctx: Execution context with waitUntil() and passThroughOnException()
return new Response('Hello World');
}
};
request: Incoming Request object (Web API standard)
request.url - Full URL stringrequest.method - HTTP method (GET, POST, etc.)request.headers - Headers objectrequest.body - ReadableStream of request bodyrequest.cf - Cloudflare-specific propertiesenv: Environment object containing bindings
env.MY_KVenv.MY_DBenv.MY_BUCKETenv.MY_DOenv.MY_SECRETenv.MY_VARctx: Execution context
ctx.waitUntil(promise) - Extend execution lifetime for async tasksctx.passThroughOnException() - Pass request to origin if Worker throwsMust return a Response object or a Promise that resolves to a Response:
// Direct return
return new Response('Hello', { status: 200 });
// Async return
return await fetch('https://api.example.com');
// With headers
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: {
'Content-Type': 'application/json'
}
});
Common routing patterns for multi-route Workers:
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Path-based routing
if (url.pathname === '/api/users') {
return handleUsers(request, env);
}
if (url.pathname.startsWith('/api/')) {
return handleAPI(request, env);
}
// Method-based routing
if (request.method === 'POST') {
return handlePost(request, env);
}
// Default
return new Response('Not Found', { status: 404 });
}
};
See examples/fetch-handler-patterns.js for complete routing examples.
Access request properties:
const url = new URL(request.url);
const method = request.method;
const headers = request.headers.get('Authorization');
const cookies = request.headers.get('Cookie');
// Cloudflare-specific properties
const country = request.cf?.country;
const colo = request.cf?.colo; // Data center code
Parse request bodies based on content type:
// JSON
const data = await request.json();
// Form data
const formData = await request.formData();
const field = formData.get('fieldName');
// Text
const text = await request.text();
// Array buffer
const buffer = await request.arrayBuffer();
// Text response
return new Response('Hello World');
// JSON response
return new Response(JSON.stringify({ message: 'Success' }), {
headers: { 'Content-Type': 'application/json' }
});
// HTML response
return new Response('<h1>Hello</h1>', {
headers: { 'Content-Type': 'text/html' }
});
// Status codes
return new Response('Not Found', { status: 404 });
return new Response('Created', { status: 201 });
// Set headers
const headers = new Headers({
'Content-Type': 'application/json',
'Cache-Control': 'max-age=3600',
'X-Custom-Header': 'value'
});
return new Response(body, { headers });
// CORS headers
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
};
// 301 permanent redirect
return Response.redirect('https://example.com', 301);
// 302 temporary redirect
return Response.redirect('https://example.com', 302);
Bindings provide access to Cloudflare resources and are configured in wrangler.toml or wrangler.jsonc.
See references/bindings-guide.md for complete binding configuration and usage patterns.
KV (Key-Value):
// Read
const value = await env.MY_KV.get('key');
const json = await env.MY_KV.get('key', 'json');
// Write
await env.MY_KV.put('key', 'value');
await env.MY_KV.put('key', JSON.stringify(data), {
expirationTtl: 3600 // Expire in 1 hour
});
D1 (Database):
// Query
const result = await env.MY_DB.prepare(
'SELECT * FROM users WHERE id = ?'
).bind(userId).first();
// Insert
await env.MY_DB.prepare(
'INSERT INTO users (name, email) VALUES (?, ?)'
).bind(name, email).run();
R2 (Object Storage):
// Read
const object = await env.MY_BUCKET.get('file.txt');
const text = await object.text();
// Write
await env.MY_BUCKET.put('file.txt', 'content');
export default {
async fetch(request, env, ctx) {
try {
// Your logic here
const result = await processRequest(request, env);
return new Response(JSON.stringify(result), {
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
console.error('Error:', error);
return new Response(JSON.stringify({
error: error.message
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}
};
function errorResponse(message, status = 500) {
return new Response(JSON.stringify({ error: message }), {
status,
headers: { 'Content-Type': 'application/json' }
});
}
// Usage
if (!apiKey) {
return errorResponse('API key required', 401);
}
See examples/error-handling.js for comprehensive error handling patterns.
Use ctx.waitUntil() to perform background tasks that extend beyond the response:
export default {
async fetch(request, env, ctx) {
// Respond immediately
const response = new Response('Request received');
// Continue processing in background
ctx.waitUntil(
logRequest(request, env)
);
return response;
}
};
async function logRequest(request, env) {
await env.MY_DB.prepare(
'INSERT INTO logs (url, timestamp) VALUES (?, ?)'
).bind(request.url, Date.now()).run();
}
Important: waitUntil extends execution but doesn't guarantee completion. Use for non-critical tasks like logging, analytics, or cache warming.
Promise.all() for concurrent operationsconsole.log() for debugging (visible in wrangler tail)wrangler dev before deployingwrangler tail to see production logsenv, not globalsWorkers support standard Web APIs and Cloudflare-specific extensions.
See references/runtime-apis.md for complete API documentation and examples.
Workers fully support TypeScript with official type definitions:
export interface Env {
MY_KV: KVNamespace;
MY_DB: D1Database;
MY_BUCKET: R2Bucket;
MY_SECRET: string;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
// Type-safe access to bindings
const value = await env.MY_KV.get('key');
return new Response(value);
}
};
Install types: npm install -D @cloudflare/workers-types
For detailed information, consult:
references/runtime-apis.md - Complete Workers runtime API documentation with examplesreferences/bindings-guide.md - All binding types with configuration and usage patternsWorking examples in examples/:
fetch-handler-patterns.js - Common routing and request handling patternserror-handling.js - Comprehensive error handling strategiesFor the latest documentation:
Use the cloudflare-docs-specialist agent to search documentation and fetch the latest information.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience