我如何访问从客户端 JS 获取的文本响应以进行 fastify?

How can I access the text response of a fetch from client JS to fastify?

我有一个 fastify node.js 应用程序,我可以在返回到调用浏览器 JS 之前看到承诺的文本结果。当该承诺返回给浏览器 JS 时,我只从承诺文本中得到空字符串。我假设承诺没有链接,这是一个没有其他内容的新承诺。如果那是正确的,我将如何访问内部承诺结果?

我已经在 fastify 应用程序的模块之间传递了 promises,在任何时候都没有问题得到结果,我只是不明白我现在做错了什么。这些是我在通话双方尝试做的基本工作:

// node.js
fastify.get('/promise', async function(request, reply) {
    var results = await someFunction(request)
    console.log(await results.text()) // this displays results as XML
    return results
})

// call to fastify app from browser JS
async function getPromise(params) {
    var response = await fetch("http://localhost:3000/promise" + params, { mode: 'no-cors' })
    console.log(await response.text()) // this is empty
}

{ mode: 'no-cors' }is blocking you to access the response because it is opaque

An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list, status is 0, status message is the empty byte sequence, header list is empty, and body is null.

这里是一个完整的例子:

'use strict'

const fetch = require('node-fetch')
const fastify = require('fastify')({ logger: true })
const fastifyCors = require('fastify-cors')

fastify.register(fastifyCors, {
  credentials: true,
  origin: '*'
})

fastify.get('/promise', async function (request, reply) {
  const results = await fetch('http://www.mocky.io/v2/5e738e46300000fd9b2e66ae')
  return results.text()
})

fastify.listen(3000)

在浏览器中:

await fetch("http://localhost:3000/promise").then(res => res.text())

它将打印HELLO WORLD