Respond with another site
Respond to the Worker request with the response from another website (example.com in this example).
export default { async fetch(request) { async function MethodNotAllowed(request) { return new Response(`Method ${request.method} not allowed.`, { status: 405, headers: { Allow: "GET", }, }); } // Only GET requests work with this proxy. if (request.method !== "GET") return MethodNotAllowed(request); return fetch(`https://example.com`); },};
Run Worker in Playgroundexport default { async fetch(request): Promise<Response> { async function MethodNotAllowed(request) { return new Response(`Method ${request.method} not allowed.`, { status: 405, headers: { Allow: "GET", }, }); } // Only GET requests work with this proxy. if (request.method !== "GET") return MethodNotAllowed(request); return fetch(`https://example.com`); },} satisfies ExportedHandler;
from js import Response, fetch, Headers
def on_fetch(request): def method_not_allowed(request): msg = f'Method {request.method} not allowed.' headers = Headers.new({"Allow": "GET"}.items) return Response.new(msg, headers=headers, status=405)
# Only GET requests work with this proxy. if request.method != "GET": return method_not_allowed(request)
return fetch("https://example.com")
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark