Browse Source

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: https://github.com/nodejs/node/pull/5248
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
process-exit-stdio-flushing
James M Snell 9 years ago
parent
commit
9b6440a86c
  1. 17
      doc/api/http.markdown

17
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. `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' ### Event: 'close'
`function () { }` `function () { }`

Loading…
Cancel
Save