javascript - 我在节点 js api 中得到 ERR_STREAM_WRITE_AFTER_END。如何解决这个问题?
问题描述
当我从 ui 中点击上传文件 api 时,我遇到了下面给出的问题。我从 react js 中点击了一个 node js api,然后从 node js 中点击了公共 api。
节点版本:- 10.15.3
npm 版本:- 6.4.1
api
router.route("/uploadLocaleFile").post(function(req, res, next) {
req.on("data", data => {
uploadFile(req, data, res);
});
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
解决方案
试试这个代码
router.route("/uploadLocaleFile").post(function(req, res, next) {
const chunks = [];
req
.on('data', data => chunks.push(data))
.on("end", _ => uploadFile(req, Buffer.concat(chunks), res)
;
});
function uploadFile(req, data, res) {
request
.post({
uri: `${notebookPath}/localfile/${req.query.experimentId}`,
body: data,
headers: {
"Content-Type": req.headers["content-type"],
Authorization: req.headers["authorization"]
}
})
.pipe(res);
}
推荐阅读
- c# - 从 .sln 整体构建通配符驱动的项目组到各自的输出目录
- c - 从文件读取后从标准输入读取
- vue.js - 将边动态添加到 VisJS 图形
- swift - 我想将图像名称连接为带有 URL 的字符串并希望在 UIImageView 中显示
- php - 如何使用behat3中的xpath和类单击嵌套div下的按钮
- c++ - 预取大量参考数据的实际限制
- docker - Docker 容器停止后不会重新启动
- javascript - 将具有多个大写单词的文本转换为具有一个大写和其他小写单词的文本
- node.js - 内存不足异常 - 未处理的 RangeError
- javascript - Javascript:更改嵌套数据结构