From 318fcf8188695f7e633bf3b13a909b2b96bb2179 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Fri, 8 May 2020 12:27:52 +0200 Subject: [PATCH] doc: add note about clientError writable handling PR-URL: https://github.com/nodejs/node/pull/33308 Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- doc/api/http.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/api/http.md b/doc/api/http.md index 039b83616f3a0c..e9180a7d375d94 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1003,6 +1003,21 @@ ensure the response is a properly formatted HTTP response message. correctly; * `rawPacket`: the raw packet of current request. +In some cases, the client has already received the response and/or the socket +has already been destroyed, like in case of `ECONNRESET` errors. Before +trying to send data to the socket, it is better to check that it is still +writable. + +```js +server.on('clientError', (err, socket) => { + if (err.code === 'ECONNRESET' || !socket.writable) { + return; + } + + socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); +}); +``` + ### Event: `'close'`