5
5
## Introduction
6
6
7
7
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
9
9
detect unintentional client disconnection scenarios such as when the client's
10
10
internet connection is interrupted. This guide covers methods to detect if
11
11
and when a client intentionally aborts a request.
12
12
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
14
14
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
16
16
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.
18
18
19
19
## Solution
20
20
@@ -32,12 +32,6 @@ logging purposes or halting business logic.
32
32
33
33
### Hands-on
34
34
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
-
41
35
Say we have the following base server set up:
42
36
43
37
``` js
@@ -88,19 +82,15 @@ Our code is setting up a Fastify server which includes the following
88
82
functionality:
89
83
90
84
- Accepting requests at http://localhost:3000 , with a 3 second delayed response
91
- of { ok: true }.
85
+ of ` { ok: true } ` .
92
86
- An onRequest hook that triggers when every request is received.
93
87
- 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.
95
89
96
90
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 ) .
104
94
105
95
You can also perform this logic outside of a hook, directly in a specific route.
106
96
@@ -175,6 +165,6 @@ in an onRequest hook or directly in an individual route.
175
165
This approach will not trigger in the event of internet disruption, and such
176
166
detection would require additional business logic. If you have flawed backend
177
167
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
179
169
logic, is not intended to handle this scenario and will not trigger when the
180
170
client aborts a request.
0 commit comments