node.js - 使用 Axios 从 Express 应用程序返回流——“提供的值‘流’不是 XMLHttpRequestResponseType 类型的有效枚举值。”
问题描述
我正在学习 Node、Express 和 React,并尝试将用于调用我的 Express API 的简单通用 Fetch 方法转换为 Axios。当我希望 Axios 自动将结果作为 JSON 数据返回时,我已经让它工作了,但在这里我希望它返回一个ReadableStream
. 从文档中它应该很简单:只需将字段添加responseType:'stream'
到config
但我不断收到错误The provided value 'stream' is not a valid enum value of type XMLHttpRequestResponseType.
我看到其他人有这个问题(见https://github.com/axios/axios/issues/1474)但我没有看不到任何解决方案。有谁知道是否有一个,或者我做错了什么?相关代码如下。感谢您的任何见解!
const logic = {
_call(path, method, headers, body, expectedStatus) {
const config = { method,responseType:'stream', url: `http://localhost:8080/${path}`}
if (headers) config.headers = headers
if (body) config.data = body
return axios( config)
.then(res => {
if (res.status === expectedStatus) {
return res
} else
return res
.then(({ message }) => {
throw new Error(message)
})
})
}
}
解决方案
推荐阅读
- angular - 如何重置可观察的计时器
- r - 将像素中的对象位置转换为地理坐标
- python - 使用 QAbstractListModel 在默认 QListView 中绘制项目的“图标”空间?
- r - 将列表转换为具有多列的 data.frame
- ruby-on-rails - 为什么应用程序记录会更改我的 RSpec 测试结果
- python - 我可以通过仅传递 ec2-instance 的私有 IP 来使用 boto 3 获取给定实例的区域值吗
- repository - 是否可以在 NXRM 中为 helm 托管 repo 创建内容选择器
- gem5 - 如何在 gem5-20 中使用 m5 将它与我自己的 C++ 程序链接?
- c# - 基于端点的路由.net core 3.1中缺少端点
- java - 将 JSON 数组添加到现有的 JSON 文件