From 9b6440a86c3e82b529b65364ba505887db6b762f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 15 Feb 2016 14:48:00 -0800 Subject: [PATCH] doc: add simple http clientError example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The clientError event allows proper http 4xx responses to be returned when a parse error occurs, but the documentation did not demonstrate how to use it. PR-URL: https://github.com/nodejs/node/pull/5248 Reviewed-By: Fedor Indutny Reviewed-By: Сковорода Никита Андреевич --- doc/api/http.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 4108080a50..a6475f3af4 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -464,6 +464,23 @@ Default behavior is to destroy the socket immediately on malformed request. `socket` is the [`net.Socket`][] object that the error originated from. +```js +const http = require('http'); + +const server = http.createServer((req, res) => { + res.end(); +}); +server.on('clientError', (err, socket) => { + socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); +}); +server.listen(8000); +``` + +When the `'clientError'` event occurs, there is no `request` or `response` +object, so any HTTP response sent, including response headers and payload, +*must* be written directly to the `socket` object. Care must be taken to +ensure the response is a properly formatted HTTP response message. + ### Event: 'close' `function () { }`