首页 > 解决方案 > 我在节点 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);
}

标签: javascriptnode.jsexpress

解决方案


试试这个代码

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);
}

推荐阅读