Bruno Miranda
Bruno Miranda
CDCloudflare Developers
Created by Bruno Miranda on 10/22/2024 in #workers-help
Workers with route configured are not being triggered
I need to do some host rewriting along header updates and was trying to use workers for that. I've configured it to *domain.com/* and enabled 100% of the logs. But so far I've only seem logs when my requests are made directly to the worker, not on domain.com. Any pointers to why they might not being triggered? Here's my code so far:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)

console.log("Hostname:", url.hostname);

url.hostname = 'other_domain.com'

const modifiedRequest = new Request(url, request)
modifiedRequest.headers.set('Host', url.hostname)

return fetch(modifiedRequest)
}

export default {
async fetch(request, env, ctx) {
return handleRequest(request);
},
};
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)

console.log("Hostname:", url.hostname);

url.hostname = 'other_domain.com'

const modifiedRequest = new Request(url, request)
modifiedRequest.headers.set('Host', url.hostname)

return fetch(modifiedRequest)
}

export default {
async fetch(request, env, ctx) {
return handleRequest(request);
},
};
8 replies