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.

PR-URL: #5248
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
  • Loading branch information
jasnell committed Feb 17, 2016
1 parent c1b3d78 commit 9b6440a
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

5 comments on commit 9b6440a

@bsnote
Copy link
Contributor

@bsnote bsnote commented on 9b6440a Feb 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work unfortunately. Making this request

curl http://127.0.0.1:8000 -H "content-length: a"

causes server to fail:

_stream_writable.js:173
  if (!(chunk instanceof Buffer) &&
                         ^

RangeError: Maximum call stack size exceeded
    at validChunk (_stream_writable.js:173:26)
    at Socket.Writable.write (_stream_writable.js:205:12)
    at Socket.write (net.js:620:40)
    at Socket.Writable.end (_stream_writable.js:433:10)
    at Socket.end (net.js:402:31)
    at Server.<anonymous> (/home/ec2-user/1.js:8:10)
    at emitTwo (events.js:105:20)
    at Server.emit (events.js:185:7)
    at Socket.socketOnError (_http_server.js:355:10)
    at emitOne (events.js:90:13)

@jasnell
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bsnote ... which node.js version?

/cc @indutny

@jasnell
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing using master it works... I believe the fix for this is only available in master currently.

@bsnote
Copy link
Contributor

@bsnote bsnote commented on 9b6440a Feb 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been testing using node.js 5.6.0. Is there any other way to return 4xx response using node.js 5.x, when a parse error occurs?

@jasnell
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bsnote ... unfortunately no. The changes with 'clientError' are semver-major and have only landed in master. It's unlikely that they would be backported to v5 or earlier. /cc @indutny

Please sign in to comment.