Skip to content

Commit

Permalink
doc: add simple http clientError example
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jasnell committed Feb 16, 2016
1 parent 954a4b4 commit 649c3b0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 () { }`
Expand Down

0 comments on commit 649c3b0

Please sign in to comment.