发送到客户端后无法设置标头

问题描述 投票:0回答:1

所以我正在开发我的个人项目,由于某种原因,我的应用程序的 Nodejs 部分不断崩溃。我不知道我一直试图找到代码中的错误的原因,但到目前为止,他们还没有取得特别的进展。 错误是 -

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:406:5)        
    at ServerResponse.setHeader (node:_http_outgoing:652:11)
    at ServerResponse.header (C:\Users\devgarg\Downloads\Projects\React Projects\CENACLE\server\node_modules\express\lib\response.js:776:10)
    at ServerResponse.json (C:\Users\devgarg\Downloads\Projects\React Projects\CENACLE\server\node_modules\express\lib\response.js:264:10)
    at PassThrough.<anonymous> (file:///C:/Users/devgarg/Downloads/Projects/React%20Projects/CENACLE/server/controller/post.js:32:17)
    at PassThrough.emit (node:events:526:35)
    at PassThrough.emit (node:domain:488:12)
    at Request.<anonymous> (C:\Users\devgarg\Downloads\Projects\React Projects\CENACLE\server\node_modules\aws-sdk\lib\request.js:590:14)
    at Request.callListeners (C:\Users\devgarg\Downloads\Projects\React Projects\CENACLE\server\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (C:\Users\devgarg\Downloads\Projects\React Projects\CENACLE\server\node_modules\aws-sdk\lib\sequential_executor.js:78:10) {
  code: 'ERR_HTTP_HEADERS_SENT',
  time: 2024-04-24T17:46:46.189Z
}

Node.js v20.9.0

显示错误在

server -> controller -> post.js
line 32
处。 显示错误的函数是从 AWS S3 Bucket 获取图像并将其发送到客户端。如果再次执行相同的请求,我尝试停止发送响应。但这并没有解决问题。

let readStreams = {};

export const getPost = (req, res) => {
    const Key = req.path.split('/')[1];

    // If there is a read stream in progress for this key, destroy it
    if (readStreams[Key]) {
        readStreams[Key].destroy();
    }

    readStreams[Key] = getFile(Key);

    // Flag to check if response has been sent
    let responseSent = false;

    // Handle errors
    readStreams[Key].on("error", (err) => {
        if (!responseSent) {
            res.json({ error: "An error occurred while reading the stream" });
            responseSent = true;
        }
    });

    // Pipe the stream to the response
    readStreams[Key].pipe(res);

    // Close the stream after it's finished processing
    readStreams[Key].on("end", () => {
        if (!responseSent) {
            readStreams[Key].destroy();
            responseSent = true;
        }
        delete readStreams[Key]; // Remove the read stream once finished
    });
}

这是显示错误的功能,但我不确定这是否真的是根本原因。

如果需要,您可以在我的 github 上查看完整代码:- https://github.com/Dev771/CENACLE

我尝试限制传入的图像请求,并且仅解决最后传入的请求。 我启用了错误处理,以防它们因流而崩溃,但这也没有解决问题。

javascript reactjs node.js amazon-s3
1个回答
0
投票

let readStreams = {};

export const getPost = (req, res) => {
    const Key = req.path.split('/')[1];

    // If there is a read stream in progress for this key, destroy it
    if (readStreams[Key]) {
        readStreams[Key].destroy();
    }

    readStreams[Key] = getFile(Key);

    // Flag to check if response has been sent
    let responseSent = false;

    const handleStreamError = (err) => {
        if (!responseSent && !res.headersSent) {
            res.status(500).json({ error: "An error occurred while reading the stream" });
            responseSent = true;
            readStreams[Key].destroy(); // Ensure the stream is destroyed
        }
    };

    // Handle errors
    readStreams[Key].on("error", handleStreamError);

    // Pipe the stream to the response
    readStreams[Key].pipe(res);

    // Handle when the stream ends
    readStreams[Key].on("end", () => {
        if (!responseSent) {
            readStreams[Key].destroy();
            responseSent = true;
        }
        delete readStreams[Key]; // Remove the read stream once finished
    });
};

© www.soinside.com 2019 - 2024. All rights reserved.