AbstractDefine CORS configuration for this route handler. Override this method to enable CORS with dynamic configuration based on the request. The same config is automatically applied to both OPTIONS preflight and actual responses.
Return undefined to use the router's default CORS config (if any).
class MyHandler extends RouteHandler<Env> {
cors(ctx): CorsConfig | undefined {
const origin = ctx.request.headers.get('Origin');
if (origin?.endsWith('.myapp.com')) {
return { origins: origin, credentials: true };
}
return undefined; // Use router default or no CORS
}
async get(ctx) {
return { data: 'hello' };
}
}
Base class for route handlers in WorkerRouter
Extend this class to create handlers for specific routes. By default, all HTTP methods throw a 405 Method Not Allowed error. Override the methods you want to support.
Response customization:
ctx.responseto modify status, statusText, or headers before returning dataResponseobject directly for full controlExample