首页 > 解决方案 > 使用 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)
                        })
            })
    }
}

标签: node.jsexpressaxios

解决方案


推荐阅读