【问题标题】:Cannot set headers after they are sent to the client - Nodejs + AWS-S3 getObject将标头发送到客户端后无法设置标头-Nodejs + AWS-S3 getObject
【发布时间】:2021-12-12 02:00:13
【问题描述】:

我将NodejsAWS-S3 一起使用,但有时会出现错误:

[ERR_HTTP_HEADERS_SENT]:无法设置标头后发送到 客户

我有这个功能可以下载任何文件。当我在某些请求后调用它时,我得到了错误。

export const downloadFile = async(req: Request, res: Response) => {
  res.setHeader('Content-Disposition', 'attachment');

  const params = {
    Bucket: AWS_S3.Bucket,
    Key: req.params.key
  };

  s3.getObject(params)
    .createReadStream()
    .on('error', error => {
      return res.status(500).json({
        message: 'An error ocurred...',
        error
      });
    }).pipe(res);
}

【问题讨论】:

    标签: node.js amazon-s3 response-headers


    【解决方案1】:

    当您在已发送 res 后尝试发送 res 时会发生此错误。你确定你的

    return res.status(500).json({
            message: 'An error ocurred...',
            error
    });
    

    函数结束后没有返回值吗?我会确保在到达方法结束之前等待您的流的执行。否则你会在本地收到一些 OK 我们在服务器上没有实际完成的情况下完成,然后尝试再次发送结果。

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2022-01-11
      • 2021-06-18
      • 2021-01-01
      相关资源
      最近更新 更多