简体   繁体   English

Node.JS:处理postdata读错误的正确方法是什么?

[英]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.

相关问题 在Node.js中,使用async / await处理错误的正确方法是什么? - In Node.js, what's the correct way to handle an error using async / await? 在node.js中使用async / await处理承诺拒绝的正确方法是什么? - What is the correct way to handle promise rejections with async/await in node.js? 处理Node.js + Express应用程序中所有错误的最佳正确方法是什么? - What's the best proper way to handle all errors in Node.js + Express app? Node.js护照将postdata传递给failureRedirect - Node.js passport pass postdata on to failureRedirect 启动和停止简单 Node.js 脚本的正确方法是什么 - What is the correct way to start and stop simple Node.js scripts 使用Node.js和TypeScript的正确方法是什么? - What's a correct way to work with Node.js and TypeScript? 处理Node.js Flash消息的最佳方法是什么? - What's the best way to handle Node.js flash messages? 使用Node.js MySQL库的正确方法是什么? - What is the correct way to be using the Node.js MySQL Library? 处理node.js私有模块依赖项的推荐方法是什么? - What is the recommended way to handle node.js private module dependencies? 在 Node.JS 中处理错误的正确方法是什么? - What is the proper way to handle an error in Node.JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM