gpt4 book ai didi

javascript - HTTP 响应流是否需要 Node.js 中的错误事件处理程序?

转载 作者:搜寻专家 更新时间:2023-11-01 00:50:00 25 4
gpt4 key购买 nike

我尝试了很多实验,但我无法让 HTTP OutgoingMessage 触发 error 事件。但我知道 OutgoingMessage 基本上是一个 WritableStream,因此容易出现错误

还有,当我读到这个官方Node.js docs about Anatomy of an HTTP transaction ,建议处理响应流上的错误。

如果是,什么情况下会触发 error 事件?我应该默默地吞下这些错误吗?

最佳答案

这周我一直在想同样的事情,但我似乎真的找不到任何直接的答案。

我搜索了 Koa看看他们做了什么,看起来他们根本没有将错误处理程序附加到 res 流。

任何可能导致流错误的情况都很少见,或者不会像 Node 教程建议的那样使您的程序崩溃。如果是这样,Koa 将无法使用,但事实并非如此,因为很多人都在使用它。

我查看了其他 HTTP 框架,如 Hapi 和 Express,但它们的代码库非常复杂,我发现很难得出类似的结论。 :\

我还找到了this几年前的一组实验似乎表明 Node 的教程与 Node 的实际行为并不完全一致:

While this experimentation may not be an exhaustive look at all the things that can go wrong in network programming, the results here seem to indicate that an HTTP response stream will never emit an error event; or, at the very least, that such an error event will never crash the server. So, while you have to bind an "error" event handler to just about every other streams in Node.js, it would seem that you can safely assume the HTTP response stream will never error, no matter what happens. And, that even if the response stream closes, you can continue to write to it / pipe to it without consequence.

对于我自己的项目,我只是将错误记录在某处,以防万一。

关于javascript - HTTP 响应流是否需要 Node.js 中的错误事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com