首页 > 解决方案 > Cloudflare worker 用于重定向、无限循环

问题描述

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);
      }
}

我想将用户重定向到 response.destination url,但它经常将用户重定向到,所以它崩溃了。我认为从 mongo 获取数据时会触发 fetch eventlistener,但我找不到其他方法来执行此操作示例链接: 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)}
    
        
}
第二个片段是当前状态,但仍多次重定向到目的地,因此重定向失败

标签: javascriptredirectcloudflareurl-shortenercloudflare-workers

解决方案


相信剩下的问题是最后几行正在使用response.destination而不是result.destination. 我用以下方法启动了一个新工人,并且能够在没有路径的情况下成功测试,并且您的路径为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);
      }
}

尝试尝试一下,也许在新的工作人员/ URL 上以避免任何可能的缓存问题?


推荐阅读