Local development proxy example getting a middleware error

Hey all, I'm having this issue trying to create a worker that will work as a reverse proxy for 2 applications. Here is the simplified logic I am using:
export default {

async fetch(req): Promise<Response> {
const incomingUrl = new URL(req.url)
const handler = await buildRequestHandler(req)
return handler(TARGET_DOMAIN)
},
} satisfies ExportedHandler<Env>

async function buildRequestHandler(req: Request) {
const url = new URL(req.url)
const incomingOrigin = req.headers.get('Referer') ?? new URL(req.url).origin

return async (newOrigin: string) => {
const targetUrl = url.toString().replace(
incomingOrigin,
newOrigin,
)

return fetch(targetUrl, {
headers: {
...req.headers,
'EXTRA-HEADER': 'FOO'
},
})
}
}
export default {

async fetch(req): Promise<Response> {
const incomingUrl = new URL(req.url)
const handler = await buildRequestHandler(req)
return handler(TARGET_DOMAIN)
},
} satisfies ExportedHandler<Env>

async function buildRequestHandler(req: Request) {
const url = new URL(req.url)
const incomingOrigin = req.headers.get('Referer') ?? new URL(req.url).origin

return async (newOrigin: string) => {
const targetUrl = url.toString().replace(
incomingOrigin,
newOrigin,
)

return fetch(targetUrl, {
headers: {
...req.headers,
'EXTRA-HEADER': 'FOO'
},
})
}
}
Here is the errors I am getting locally:
No description
No description
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server