To deploy the Cloudflare Worker that returns the visitor's IP address in JSON format with CORS support using the Cloudflare dashboard (UI), follow these steps. This approach is handy if you prefer a graphical interface over command-line operations.
- Navigate to Cloudflare: Go to Cloudflare's website and log in with your account credentials.
- Select Your Domain: From the list of domains in your Cloudflare account, click on the domain you want to deploy the Worker under.
- Open the Workers Section: In your domain's dashboard, look for the "Workers" option. This can typically be found in the sidebar menu. Click on it to go to the Workers dashboard.
- Create a Worker: Click on the "Create a Worker" button. This action opens a new page where you can write or paste your Worker's code.
- Name Your Worker: You'll be given an option to name your Worker. Choose a meaningful name that reflects its functionality, such as
return-ip-with-cors
.
Copy the Worker Code: Copy the JavaScript code provided in the index.js
- Save and Deploy: After pasting and reviewing your code, click the "Save and Deploy" button to deploy your Worker immediately. Cloudflare will provide a unique URL to access your Worker.
-
Test Your Worker: Use the provided URL to test your Worker. You can visit the URL in your browser or use a tool like
curl
to make a request. You should receive a JSON response containing your IP address.Example
curl
command:curl -i https://your-worker.your-subdomain.workers.dev
Replace
https://your-worker.your-subdomain.workers.dev
with the URL provided by Cloudflare for your Worker. The-i
option includes the HTTP response headers in the output, which can be useful for checking the CORS headers.
If you want your Worker to respond to requests at a specific path on your site, you can configure a route:
- Go to the Workers Section: Return to the Workers section of the Cloudflare dashboard.
- Select Your Worker: Find your Worker in the list and click on it.
- Add Route: Click on "Add route" and specify the pattern that matches the requests you want to handle with your Worker, e.g.,
example.com/api/ip
. Set the route to match the domain and path where you want your Worker to be accessible.
Congratulations! You've successfully deployed a Cloudflare Worker through the Cloudflare UI that returns the visitor's IP address in JSON format with CORS support.