Hi there. Im absolutely new to Cloudflare. I have 2 workers. 1 is angular front-end, and another is a back-end. My problem is, I cannot make any http request from angular app to a back-end app, because of CORS. My lates code for back-end worker is below:
import { Router } from '@tsndr/cloudflare-worker-router'
export interface Env {
DB: D1Database;
}
// Initialize router
const router = new Router<Env>()
// Enabling build in CORS support
router.cors()
// Simple get
router.get('/user', () => {
return Response.json({
id: 1,
name: 'John Doe'
})
})
// Listen Cloudflare Workers Fetch Event
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
return router.handle(request, env, ctx)
}
}
For the back-end, I have tried everything I could find online, like setting CORS headers manually:
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': config.host,
'Access-Control-Allow-Methods': config.methods.join(', '),
'Access-Control-Allow-Headers': 'Content-Type',
},
})
Handle OPTIONS:
switch (request.method) {
case 'OPTIONS':
return handleOptions(request)
default:
return handleRequest(request)
}
Nothing helps. Nor I can reach the back-end when its running locally, nor when I publish worker to cloudflare. My code on a client is simple:
private readonly TEST_URL = "http://127.0.0.1:8787/user"; // or remote when published worker
await this.httpClient.post(this.TEST_URL, {}).toPromise();
Any advise?