Configure Cross-Origin Resource Sharing to allow your app to communicate with your R2 bucket.
Before using the file upload features of the application, you must configure the CORS rules for your Cloudflare R2 bucket. This enables your browser to upload files directly to the storage bucket securely.
Navigate to dash.cloudflare.com and sign in to your account.
Locate and click the search bar at the top of the dashboard (or press Cmd+K / Ctrl+K).
Type "R2" in the search box and select "R2 Object Storage" from the results required to open the R2 management UI.
You should already have a bucket created for this project. If not, create one now.
Click on the specific bucket you are using for this application (e.g., nodejs-starter-bucket) to view its details.
Navigate to the "Settings" tab near the top of the bucket details page.
Scroll down or use the sidebar navigation to find the "CORS Policy" section.
If a policy exists, click "Edit". If not, look for an "Add CORS Policy" button. This will open a code editor window.
In the AllowedOrigins array, define which domains can access your bucket.
For development or testing, you can use "*" to allow all domains (less secure).
For production, rigorously specify your deployed application's URL (e.g., "https://my-app.railway.app").
Crucial: Do NOT add a trailing slash (/) at the end of your domain URL in the allowed origins list. Click "Save" to apply the changes.
Get free tutorials, AI news & special discounts.