[英]Node.JS: What is the correct way to handle postdata read errors?
If the ServerRequest
object emits an error
event, that means that the postdata could not be fully received, most likely due to a network failure. 如果
ServerRequest
对象发出error
事件,则意味着无法完全接收postdata,很可能是由于网络故障。 This type of network failure is most likely terminal in both directions, right? 这种类型的网络故障很可能是两个方向的终端,对吧? So, how should I right my code?
那么,我应该如何改正我的代码呢?
I could say res.writeHead(400); res.end()
我可以说
res.writeHead(400); res.end()
res.writeHead(400); res.end()
But is there a smarter way? 但是有更聪明的方法吗? Perhaps one that just 'does nothing', but will not result in a memory leak / hang.
也许只是“什么都不做”,但不会导致内存泄漏/挂起。 It seems like
request.connection.destroy()
is a good idea, but I am not sure if that is safe. 似乎
request.connection.destroy()
是一个好主意,但我不确定这是否安全。
What would you recommend? 你会推荐什么?
you don't need to do anything. 你不需要做任何事情。 internally, the socket/req/res will be destroyed.
在内部,socket / req / res将被销毁。 it'll be garbage collected eventually.
它最终会被垃圾收集。
you don't need to send a response code - the connection is already destroyed so it's not going anywhere. 你不需要发送响应代码 - 连接已经被破坏,所以它不会去任何地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.