Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

別サイトのレスポンスを返す

Worker へのリクエストに、別のウェブサイト(この例では example.com)のレスポンスで応答します。

最終更新 Markdown で表示Agent セットアップ

すぐに始めたい場合は、下のボタンを選択します。

Cloudflare にデプロイ

GitHub アカウントにリポジトリを作成し、アプリケーションを Cloudflare Workers にデプロイします。

export default {
  async fetch(request) {
    function MethodNotAllowed(request) {
      return new Response(`Method ${request.method} not allowed.`, {
        status: 405,
        headers: {
          Allow: "GET",
        },
      });
    }
    // このプロキシは GET リクエストのみに対応します。
    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")

役に立ちましたか?