Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
Cloudflare worker for redirect, infinite loop

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  var pathname = url.pathname.substring(1, url.pathname.length)
  var response = await fetch('https://eu-central-1.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/1saas-functions-mmlow/service/urlRedirect/incoming_webhook/cloudWorker', {
        headers: {
          "Accept": "application/json",
          'Content-Type': 'application/json',
        },
        method: 'POST',
        body: JSON.stringify({identifier: pathname}),
      })
      const result = await response.json();
      if(response.destination) {
        return Response.redirect(response.destination, 302);
      } else {
        return Response.redirect("https://1saas.co", 302);
      }
}

I want to redirect the user to the response.destination url but it redirects the user to often so it crashes. I think the fetch eventlistener gets triggered when fetching data from mongo, but i cant find a other way to do it example link: http://lyl.ai/6j6P6Zwr

addEventListener('fetch', event => {
    try{event.respondWith(handleRequest(event.request))} catch(e){console.log(e)}

})


async function handleRequest(request){
    try{
        const url = new URL(request.url)
        var pathname = url.pathname.substring(1, url.pathname.length)
        /*var response = await fetch('https://eu-central-1.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/1saas-functions-mmlow/service/urlRedirect/incoming_webhook/cloudWorker', {
            headers: {
                "Accept": "application/json",
                'Content-Type': 'application/json',
            },
            method: 'POST',
            body: JSON.stringify({identifier: pathname}),
            })*/
        
        if(true){
            return Response.redirect("https://1saas.co", 302);
        } else {
            return Response.redirect("https://1saas.co", 302);
        }
    }catch(e)
    {console.log(e.message)}
    
        
}
second snippet is the current status but still redirecting to many times to destination so redirect fails

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Believe the remaining issue is that the last couple of lines are using response.destination instead of result.destination. I spun up a new worker with the following, and was able to succesfully test with no path, and with your path of 6j6P6Zwr -

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  var pathname = url.pathname.substring(1, url.pathname.length)
  var response = await fetch('https://eu-central-1.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/1saas-functions-mmlow/service/urlRedirect/incoming_webhook/cloudWorker', {
        headers: {
          "Accept": "application/json",
          'Content-Type': 'application/json',
        },
        method: 'POST',
        body: JSON.stringify({identifier: pathname}),
      })
      const result = await response.json();
      if(result.destination) {
        return Response.redirect(result.destination, 302);
      } else {
        return Response.redirect("https://1saas.co", 302);
      }
}

Try giving this a shot, maybe on a new worker / URL to avoid any possible caching issues?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda