Skip to content

Commit 7829f48

Browse files
Docs: Update guide about detecting client abort (fastify#4530)
* suggested updates from: fastify#4518 * update text to code format * remove details about different properties
1 parent ad79204 commit 7829f48

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

docs/Guides/Detecting-When-Clients-Abort.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
## Introduction
66

77
Fastify provides request events to trigger at certain points in a request's
8-
lifecycle. However, there isn't a mechanism built-in to
8+
lifecycle. However, there isn't a built-in mechanism to
99
detect unintentional client disconnection scenarios such as when the client's
1010
internet connection is interrupted. This guide covers methods to detect if
1111
and when a client intentionally aborts a request.
1212

13-
Keep in mind, Fastify's clientErrorHandler is not designed to detect when a
13+
Keep in mind, Fastify's `clientErrorHandler` is not designed to detect when a
1414
client aborts a request. This works in the same way as the standard Node HTTP
15-
module, which triggers the clientError event when there is a bad request or
15+
module, which triggers the `clientError` event when there is a bad request or
1616
exceedingly large header data. When a client aborts a request, there is no
17-
error on the socket and the clientErrorHandler will not be triggered.
17+
error on the socket and the `clientErrorHandler` will not be triggered.
1818

1919
## Solution
2020

@@ -32,12 +32,6 @@ logging purposes or halting business logic.
3232

3333
### Hands-on
3434

35-
For this sample solution, we'll be using the following:
36-
37-
- `node.js v18.12.1`
38-
- `npm 8.19.2`
39-
- `fastify 4.11.0`
40-
4135
Say we have the following base server set up:
4236

4337
```js
@@ -88,19 +82,15 @@ Our code is setting up a Fastify server which includes the following
8882
functionality:
8983

9084
- Accepting requests at http://localhost:3000, with a 3 second delayed response
91-
of { ok: true }.
85+
of `{ ok: true }`.
9286
- An onRequest hook that triggers when every request is received.
9387
- Logic that triggers in the hook when the request is closed.
94-
- Logging that occurs when the closed request attribute 'aborted' is true.
88+
- Logging that occurs when the closed request property `aborted` is true.
9589

9690
In the request close event, you should examine the diff between a successful
97-
request and one aborted by the client to determine the best attribute for your
98-
use case. There are many other attributes on a request that will differ between
99-
a successfully closed request and one that has been aborted by the client.
100-
Examples of such attributes include:
101-
102-
- destroyed
103-
- errors
91+
request and one aborted by the client to determine the best property for your
92+
use case. You can find details about request properties in the
93+
[NodeJS documentation](https://nodejs.org/api/http.html).
10494

10595
You can also perform this logic outside of a hook, directly in a specific route.
10696

@@ -175,6 +165,6 @@ in an onRequest hook or directly in an individual route.
175165
This approach will not trigger in the event of internet disruption, and such
176166
detection would require additional business logic. If you have flawed backend
177167
application logic that results in a server crash, then you could trigger a
178-
false detection. The clientErrorHandler, either by default or with custom
168+
false detection. The `clientErrorHandler`, either by default or with custom
179169
logic, is not intended to handle this scenario and will not trigger when the
180170
client aborts a request.

0 commit comments

Comments
 (0)