【问题标题】:Cloudflare worker for redirect, infinite loopCloudflare worker 用于重定向、无限循环
【发布时间】:2021-12-17 16:23:39
【问题描述】:

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)}
    
        
}
第二个 sn-p 是当前状态,但仍多次重定向到目的地,因此重定向失败

【问题讨论】:

  • 由于错误发生在工作脚本本身中,我会转到快速编辑工具(如果使用 CLI,则使用 wrangler)并检查那里是否有任何错误。您还可以添加 console.logs 来调试问题 - developers.cloudflare.com/workers/learning/debugging-workers 对于 QuickEditor(Web 界面),它将在底部右侧窗格的控制台中显示错误/日志。仅从代码 sn-p 本身来看,我看不到任何地方定义的函数 redirectTest(第 2 行)。那么,这可能是引发错误。
  • 我在事件监听器中有错误的功能,但我现在只想如果有重定向的方法,我会得到很多重定向发生的错误
  • 随着函数的更新,我猜这个问题与这一行有关 - response = await response.json(); 您可能想在此处尝试使用新变量(而不是重用 response)。我还将控制台记录结果并确保您获得了您期望的值。
  • 我得到了我期望的值,我认为每次我在 handleRequest 函数中获取时都会调用 eventListener,这会导致错误,但我不知道如何避免这个问题
  • 它不应该...但是可以通过删除 fetch 行并硬编码您的响应目标值来轻松测试这是否是问题所在。您上面更新的代码仍在使用响应变量而不是新的result btw,不确定这是否是它实际运行的?

标签: javascript redirect cloudflare url-shortener cloudflare-workers


【解决方案1】:

相信剩下的问题是最后几行使用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 上以避免任何可能的缓存问题?

【讨论】:

  • 谢谢你试一试
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 2012-06-08
  • 2019-08-31
相关资源
最近更新 更多