Skip to content

Commit 9b6440a

Browse files
committed
doc: add simple http clientError example
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: #5248 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
1 parent c1b3d78 commit 9b6440a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

doc/api/http.markdown

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,23 @@ Default behavior is to destroy the socket immediately on malformed request.
464464

465465
`socket` is the [`net.Socket`][] object that the error originated from.
466466

467+
```js
468+
const http = require('http');
469+
470+
const server = http.createServer((req, res) => {
471+
res.end();
472+
});
473+
server.on('clientError', (err, socket) => {
474+
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
475+
});
476+
server.listen(8000);
477+
```
478+
479+
When the `'clientError'` event occurs, there is no `request` or `response`
480+
object, so any HTTP response sent, including response headers and payload,
481+
*must* be written directly to the `socket` object. Care must be taken to
482+
ensure the response is a properly formatted HTTP response message.
483+
467484
### Event: 'close'
468485

469486
`function () { }`

0 commit comments

Comments
 (0)