简体   繁体   English

Node,Express 响应正文是 Readablestream 而不是 JSON

[英]Node,Express response body is Readablestream instead of JSON

Creating an API endpoint using Express.使用 Express 创建一个 API 端点。 Usually responding with res.json() sends a JSON object in the response, which can consume in the client the same way.通常使用res.json()响应会在响应中发送 JSON object ,它可以在客户端以相同的方式消费。

I am using a batched promise in the API, resolving the promises (I think), and sending back an object.我在 API 中使用批处理的 promise,解决了承诺(我认为),然后发回了 object。

Why is a JSON object not available client-side - instead I'm receiving a Readablestream in the body.为什么 JSON object 在客户端不可用 - 相反我在正文中收到了一个可读流。

Below are the helper function and response, as well as what the client receives.下面是助手 function 和响应,以及客户端收到的内容。

const addCoinVolumeData = async (data) => {
  const promises = data.map(async datum => {
    const volumeData = await fetchCoinGeckoCoinVolume(datum.id);
    const volumeDelta = calculateCoinVolumeDelta(volumeData);
    datum.volume_change_24h = volumeDelta;
    return datum;
  });

  const result = await Promise.all(promises);
  return result;
}
  data = await addCoinVolumeData(data);
  res.json(data);
Response {type: 'basic', url: 'http://localhost:3001/aggregate-coin-data', redirected: false, status: 200, ok: true, …}
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"

Turns out it was actually the URL was using to fetch client-side since calling 'localhost...'.原来它实际上是 URL 用于获取客户端,因为调用'localhost ...'。 Proxy in package.json didn't resolve much. package.json 中的代理没有解决太多。 Changed the port, and for some reason that worked - don't know why since nothing else running on it (as far as was reported anyway).更改了端口,并且由于某种原因起作用了-不知道为什么,因为没有其他东西在上面运行(据报道)。 All is well.一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM