← Cloudflare Workers / workers / examples
Ответ с другого сайта
Если вы хотите быстро начать, нажмите на кнопку ниже.
Это создаёт репозиторий в вашем аккаунте GitHub и разворачивает приложение в Cloudflare Workers.
export default {
async fetch(request) {
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`);
},
};export default {
async fetch(request): Promise<Response> {
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 workers import WorkerEntrypoint, Response, fetch
class Default(WorkerEntrypoint):
def fetch(self, request):
def method_not_allowed(request):
msg = f'Method {request.method} not allowed.'
headers = {"Allow": "GET"}
return Response(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")