Express中res.send/res.json无响应体,客户端仅获ReadableStream求助
问题:Express返回数据客户端无法获取实际内容
问题现象
- Node服务器通过第三方API获取到用户订单数据后,用
console.log可以正常打印完整的订单结构:
{"customer":{"orders":{"edges":[{"node":{"id":"gid://shopify/Order/1234564564","lineItems":{"edges":[{"node":{"title":"Some title here"}}]}}}]}}}
- 但调用
res.send或res.json把数据返回给客户端时,客户端在开发者工具中打印响应,得到的是一个ReadableStream对象,看不到实际的返回内容:
body:ReadableStream locked:false [[Prototype]]:ReadableStream bodyUsed:false headers: Headers {} ok:true redirected:false status:200 statusText:"OK" type:"basic" url:"http://localhost:9000/api/getOrders?cid=gid://shopify/Customer/1234564564" [[Prototype]]:Response
- 即使测试最基础的接口,客户端得到的响应类型还是一样:
app.get('/testExpress', (req, res) => { res.send("Hello") });
解决方法
客户端使用fetch发起请求时,必须调用响应对象的解析方法来读取实际内容。比如用text()解析文本格式,或者用json()解析JSON格式:
fetch('/testExpress') .then(async (response) => { const content = await response.text(); console.log(content); })
内容的提问来源于stack exchange,提问作者gregwhitworth
相关产品推荐
相关产品推荐

