Cloudflare worker向Cloudflare DNS发出的请求未返回DNS结果

Cloudflare worker向Cloudflare DNS发出的请求未返回DNS结果,dns,cloudflare,cloudflare-workers,Dns,Cloudflare,Cloudflare Workers,我有一个Cloudflare(CF)工作程序,我想让它使用CF DNS()发出一些DNS请求 因此,一个相当基础的工人: /** * readRequestBody reads in the incoming request body * Use await readRequestBody(..) in an async function to get the string * @param {Request} request the incoming request to read fr

我有一个Cloudflare(CF)工作程序,我想让它使用CF DNS()发出一些DNS请求

因此,一个相当基础的工人:

/**
 * readRequestBody reads in the incoming request body
 * Use await readRequestBody(..) in an async function to get the string
 * @param {Request} request the incoming request to read from
 */
async function readRequestBody(request) {
  const { headers } = request
  const contentType = headers.get('content-type')

  if (contentType.includes('application/json')) {
    const body = await request.json()
    return JSON.stringify(body)
  }

  return ''
}

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
   let reqBody = await readRequestBody(request)

   var jsonTlds = JSON.parse(reqBody);

   const fetchInit = {
      method: 'GET',
    }
   let promises = []
   for (const tld of jsonTlds.tlds) {

       //Dummy request until I can work out why I am not getting the response of the DNS query
       var requestStr = 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A'

       let promise = fetch(requestStr, fetchInit)

       promises.push(promise)
   }

   try {
      let results = await Promise.all(promises)

      return new Response(JSON.stringify(results), {status: 200})
   } catch(err) {
       return new Response(JSON.stringify(err), {status: 500})
   }
}

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
我刚刚将DNS查询硬编码为:

我希望我得到的JSON结果是:

{
    "Status": 0,
    "TC": false,
    "RD": true,
    "RA": true,
    "AD": true,
    "CD": false,
    "Question": [
        {
            "name": "example.com.",
            "type": 1
        }
    ],
    "Answer": [
        {
            "name": "example.com.",
            "type": 1,
            "TTL": 9540,
            "data": "93.184.216.34"
        }
    ]
}
但是,在结果中,我得到的似乎是作为fetch()的一部分建立的websocket的结果(假设我循环一次)

所以我的问题是,我在这里做错了什么,以至于我没有从1.1.1.1 API获得DNS JSON响应?

fetch()
返回一个承诺,其中包含响应状态、头和正文流。这个物体就是你在“结果”中看到的东西。要读取响应正文,必须进行进一步调用

尝试定义如下所示的函数:

async function fetchJsonBody(req, init) {
  let response = await fetch(req, init);
  if (!response.ok()) {
    // Did not return status 200; throw an error.
    throw new Error(response.status + " " + response.statusText);
  }

  // OK, now we can read the body and parse it as JSON.
  return await response.json();
}
现在您可以更改:

let promise = fetch(requestStr, fetchInit)
致:


谢谢,但是我失去了在一个块中启动所有响应的能力,因为在fetchJsonBody()中有一个wait?@边缘否-注意,我没有在调用
fetchJsonBody()之前放置
wait
。因此,函数本身内部的
wait
s将异步执行。对
fetchJsonBody()
的外部调用立即返回,并返回一个承诺。因此,这仍然会使所有请求并行。
let promise = fetch(requestStr, fetchInit)
let promise = fetchJsonBody(requestStr, fetchInit)