fastify 4.22.2 → 4.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +30 -33
  2. package/build/build-error-serializer.js +2 -2
  3. package/build/build-validation.js +2 -2
  4. package/build/sync-version.js +2 -2
  5. package/docs/Guides/Database.md +1 -1
  6. package/docs/Guides/Delay-Accepting-Requests.md +2 -2
  7. package/docs/Guides/Ecosystem.md +3 -1
  8. package/docs/Guides/Getting-Started.md +5 -4
  9. package/docs/Guides/Migration-Guide-V4.md +18 -15
  10. package/docs/Reference/ContentTypeParser.md +0 -1
  11. package/docs/Reference/HTTP2.md +4 -4
  12. package/docs/Reference/Hooks.md +30 -1
  13. package/docs/Reference/Middleware.md +1 -1
  14. package/docs/Reference/Principles.md +1 -1
  15. package/docs/Reference/Reply.md +7 -7
  16. package/docs/Reference/Request.md +11 -7
  17. package/docs/Reference/Server.md +2 -2
  18. package/examples/asyncawait.js +3 -1
  19. package/examples/benchmark/parser.js +1 -1
  20. package/examples/hooks.js +3 -0
  21. package/examples/http2.js +5 -3
  22. package/examples/https.js +5 -3
  23. package/examples/parser.js +4 -2
  24. package/examples/shared-schema.js +3 -1
  25. package/examples/simple-stream.js +4 -2
  26. package/examples/simple.js +3 -1
  27. package/examples/typescript-server.ts +1 -0
  28. package/examples/use-plugin.js +3 -1
  29. package/fastify.d.ts +2 -2
  30. package/fastify.js +5 -7
  31. package/lib/contentTypeParser.js +4 -4
  32. package/lib/error-handler.js +1 -1
  33. package/lib/hooks.js +55 -0
  34. package/lib/pluginUtils.js +1 -1
  35. package/lib/reply.js +1 -1
  36. package/lib/request.js +15 -0
  37. package/lib/server.js +26 -10
  38. package/lib/warnings.js +8 -0
  39. package/package.json +15 -5
  40. package/test/404s.test.js +1 -1
  41. package/test/als.test.js +1 -1
  42. package/test/async-await.test.js +1 -1
  43. package/test/bodyLimit.test.js +1 -1
  44. package/test/build/error-serializer.test.js +2 -2
  45. package/test/build/version.test.js +2 -2
  46. package/test/bundler/webpack/webpack.config.js +1 -1
  47. package/test/client-timeout.test.js +1 -1
  48. package/test/close.test.js +2 -2
  49. package/test/connectionTimeout.test.js +1 -1
  50. package/test/custom-http-server.test.js +2 -2
  51. package/test/custom-parser.0.test.js +1 -1
  52. package/test/custom-querystring-parser.test.js +1 -1
  53. package/test/diagnostics-channel.test.js +3 -3
  54. package/test/fastify-instance.test.js +1 -1
  55. package/test/genReqId.test.js +1 -1
  56. package/test/helper.js +2 -2
  57. package/test/hooks-async.test.js +2 -2
  58. package/test/hooks.on-listen.test.js +1090 -0
  59. package/test/hooks.on-ready.test.js +1 -1
  60. package/test/hooks.test.js +4 -4
  61. package/test/http2/closing.test.js +3 -3
  62. package/test/http2/missing-http2-module.test.js +1 -1
  63. package/test/https/custom-https-server.test.js +2 -2
  64. package/test/inject.test.js +3 -3
  65. package/test/internals/contentTypeParser.test.js +1 -1
  66. package/test/internals/errors.test.js +2 -2
  67. package/test/internals/initialConfig.test.js +1 -1
  68. package/test/internals/logger.test.js +1 -1
  69. package/test/internals/reply.test.js +7 -7
  70. package/test/internals/reqIdGenFactory.test.js +6 -2
  71. package/test/internals/server.test.js +4 -4
  72. package/test/keepAliveTimeout.test.js +1 -1
  73. package/test/listen.deprecated.test.js +1 -1
  74. package/test/listen.test.js +19 -5
  75. package/test/maxRequestsPerSocket.test.js +1 -1
  76. package/test/plugin.name.display.js +1 -1
  77. package/test/reply-error.test.js +4 -4
  78. package/test/reply-trailers.test.js +3 -3
  79. package/test/request-error.test.js +1 -1
  80. package/test/request.deprecated.test.js +38 -0
  81. package/test/requestTimeout.test.js +1 -1
  82. package/test/route-hooks.test.js +1 -1
  83. package/test/route.test.js +1 -1
  84. package/test/router-options.test.js +5 -6
  85. package/test/serial/logger.0.test.js +2 -2
  86. package/test/serial/logger.1.test.js +5 -5
  87. package/test/skip-reply-send.test.js +1 -1
  88. package/test/stream.test.js +20 -20
  89. package/test/trust-proxy.test.js +1 -1
  90. package/test/types/hooks.test-d.ts +11 -0
  91. package/test/unsupported-httpversion.test.js +1 -1
  92. package/test/upgrade.test.js +3 -3
  93. package/test/versioned-routes.test.js +1 -1
  94. package/types/hooks.d.ts +45 -12
  95. package/types/instance.d.ts +11 -1
package/README.md CHANGED
@@ -230,39 +230,32 @@ The overhead that each framework has on your application depends on your
230
230
  application, you should __always__ benchmark if performance matters to you.
231
231
 
232
232
  ## Documentation
233
- * <a href="./docs/Guides/Getting-Started.md"><code><b>Getting
234
- Started</b></code></a>
235
- * <a href="./docs/Guides/Index.md"><code><b>Guides</b></code></a>
236
- * <a href="./docs/Reference/Server.md"><code><b>Server</b></code></a>
237
- * <a href="./docs/Reference/Routes.md"><code><b>Routes</b></code></a>
238
- * <a
239
- href="./docs/Reference/Encapsulation.md"><code><b>Encapsulation</b></code></a>
240
- * <a href="./docs/Reference/Logging.md"><code><b>Logging</b></code></a>
241
- * <a href="./docs/Reference/Middleware.md"><code><b>Middleware</b></code></a>
242
- * <a href="./docs/Reference/Hooks.md"><code><b>Hooks</b></code></a>
243
- * <a href="./docs/Reference/Decorators.md"><code><b>Decorators</b></code></a>
244
- * <a href="./docs/Reference/Validation-and-Serialization.md"><code><b>Validation
245
- and Serialization</b></code></a>
246
- * <a href="./docs/Guides/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
247
- * <a href="./docs/Reference/Lifecycle.md"><code><b>Lifecycle</b></code></a>
248
- * <a href="./docs/Reference/Reply.md"><code><b>Reply</b></code></a>
249
- * <a href="./docs/Reference/Request.md"><code><b>Request</b></code></a>
250
- * <a href="./docs/Reference/Errors.md"><code><b>Errors</b></code></a>
251
- * <a href="./docs/Reference/ContentTypeParser.md"><code><b>Content Type
252
- Parser</b></code></a>
253
- * <a href="./docs/Reference/Plugins.md"><code><b>Plugins</b></code></a>
254
- * <a href="./docs/Guides/Testing.md"><code><b>Testing</b></code></a>
255
- * <a href="./docs/Guides/Benchmarking.md"><code><b>Benchmarking</b></code></a>
256
- * <a href="./docs/Guides/Write-Plugin.md"><code><b>How to write a good
257
- plugin</b></code></a>
258
- * <a href="./docs/Guides/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
259
- * <a href="./docs/Reference/HTTP2.md"><code><b>HTTP2</b></code></a>
260
- * <a href="./docs/Reference/LTS.md"><code><b>Long Term Support</b></code></a>
261
- * <a href="./docs/Reference/TypeScript.md"><code><b>TypeScript and types
262
- support</b></code></a>
263
- * <a href="./docs/Guides/Serverless.md"><code><b>Serverless</b></code></a>
264
- * <a
265
- href="./docs/Guides/Recommendations.md"><code><b>Recommendations</b></code></a>
233
+ * [__`Getting Started`__](./docs/Guides/Getting-Started.md)
234
+ * [__`Guides`__](./docs/Guides/Index.md)
235
+ * [__`Server`__](./docs/Reference/Server.md)
236
+ * [__`Routes`__](./docs/Reference/Routes.md)
237
+ * [__`Encapsulation`__](./docs/Reference/Encapsulation.md)
238
+ * [__`Logging`__](./docs/Reference/Logging.md)
239
+ * [__`Middleware`__](./docs/Reference/Middleware.md)
240
+ * [__`Hooks`__](./docs/Reference/Hooks.md)
241
+ * [__`Decorators`__](./docs/Reference/Decorators.md)
242
+ * [__`Validation and Serialization`__](./docs/Reference/Validation-and-Serialization.md)
243
+ * [__`Fluent Schema`__](./docs/Guides/Fluent-Schema.md)
244
+ * [__`Lifecycle`__](./docs/Reference/Lifecycle.md)
245
+ * [__`Reply`__](./docs/Reference/Reply.md)
246
+ * [__`Request`__](./docs/Reference/Request.md)
247
+ * [__`Errors`__](./docs/Reference/Errors.md)
248
+ * [__`Content Type Parser`__](./docs/Reference/ContentTypeParser.md)
249
+ * [__`Plugins`__](./docs/Reference/Plugins.md)
250
+ * [__`Testing`__](./docs/Guides/Testing.md)
251
+ * [__`Benchmarking`__](./docs/Guides/Benchmarking.md)
252
+ * [__`How to write a good plugin`__](./docs/Guides/Write-Plugin.md)
253
+ * [__`Plugins Guide`__](./docs/Guides/Plugins-Guide.md)
254
+ * [__`HTTP2`__](./docs/Reference/HTTP2.md)
255
+ * [__`Long Term Support`__](./docs/Reference/LTS.md)
256
+ * [__`TypeScript and types support`__](./docs/Reference/TypeScript.md)
257
+ * [__`Serverless`__](./docs/Guides/Serverless.md)
258
+ * [__`Recommendations`__](./docs/Guides/Recommendations.md)
266
259
 
267
260
  中文文档[地址](https://github.com/fastify/docs-chinese/blob/HEAD/README.md)
268
261
 
@@ -325,6 +318,8 @@ listed in alphabetical order.
325
318
  <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
326
319
  * [__James Sumners__](https://github.com/jsumners),
327
320
  <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
321
+ * [__Aras Abbasi__](https://github.com/uzlopak),
322
+ <https://www.npmjs.com/~uzlopak>
328
323
 
329
324
  ### Fastify Plugins team
330
325
  * [__Matteo Collina__](https://github.com/mcollina),
@@ -347,6 +342,8 @@ listed in alphabetical order.
347
342
  <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
348
343
  * [__Simone Busoli__](https://github.com/simoneb),
349
344
  <https://twitter.com/simonebu>, <https://www.npmjs.com/~simoneb>
345
+ * [__Gürgün Dayıoğlu__](https://github.com/gurgunday),
346
+ <https://www.npmjs.com/~gurgunday>
350
347
 
351
348
  ### Great Contributors
352
349
  Great contributors on a specific area in the Fastify ecosystem will be invited
@@ -2,8 +2,8 @@
2
2
  'use strict'
3
3
 
4
4
  const FJS = require('fast-json-stringify')
5
- const path = require('path')
6
- const fs = require('fs')
5
+ const path = require('node:path')
6
+ const fs = require('node:fs')
7
7
 
8
8
  const code = FJS({
9
9
  type: 'object',
@@ -2,8 +2,8 @@
2
2
 
3
3
  const AjvStandaloneCompiler = require('@fastify/ajv-compiler/standalone')
4
4
  const { _ } = require('ajv')
5
- const fs = require('fs')
6
- const path = require('path')
5
+ const fs = require('node:fs')
6
+ const path = require('node:path')
7
7
 
8
8
  const factory = AjvStandaloneCompiler({
9
9
  readMode: false,
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const fs = require('fs')
4
- const path = require('path')
3
+ const fs = require('node:fs')
4
+ const path = require('node:path')
5
5
 
6
6
  // package.json:version -> fastify.js:VERSION
7
7
  const { version } = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')).toString('utf8'))
@@ -275,7 +275,7 @@ CREATE TABLE IF NOT EXISTS users (
275
275
  ```javascript
276
276
  const pg = require('pg')
277
277
  const Postgrator = require('postgrator')
278
- const path = require('path')
278
+ const path = require('node:path')
279
279
 
280
280
  async function migrate() {
281
281
  const client = new pg.Client({
@@ -135,7 +135,7 @@ follows:
135
135
 
136
136
  ```js
137
137
  const { fetch } = require('undici')
138
- const { setTimeout } = require('timers/promises')
138
+ const { setTimeout } = require('node:timers/promises')
139
139
 
140
140
  const MAGIC_KEY = '12345'
141
141
 
@@ -249,7 +249,7 @@ server.listen({ port: '1234' })
249
249
 
250
250
  ```js
251
251
  const { fetch } = require('undici')
252
- const { setTimeout } = require('timers/promises')
252
+ const { setTimeout } = require('node:timers/promises')
253
253
 
254
254
  const MAGIC_KEY = '12345'
255
255
 
@@ -122,7 +122,7 @@ section.
122
122
  serving Swagger/OpenAPI documentation for Fastify, supporting dynamic
123
123
  generation.
124
124
  - [`@fastify/throttle`](https://github.com/fastify/fastify-throttle) Plugin for
125
- - throttling the download speed of a request.
125
+ throttling the download speed of a request.
126
126
  - [`@fastify/type-provider-json-schema-to-ts`](https://github.com/fastify/fastify-type-provider-json-schema-to-ts)
127
127
  Fastify
128
128
  [type provider](https://www.fastify.io/docs/latest/Reference/Type-Providers/)
@@ -671,7 +671,9 @@ middlewares into Fastify plugins
671
671
  and lightweight Sequelize plugin for Fastify.
672
672
  - [`typeorm-fastify-plugin`](https://github.com/jclemens24/fastify-typeorm) A simple
673
673
  and updated Typeorm plugin for use with Fastify.
674
+
674
675
  #### [Community Tools](#community-tools)
676
+
675
677
  - [`@fastify-userland/workflows`](https://github.com/fastify-userland/workflows)
676
678
  Reusable workflows for use in the Fastify plugin
677
679
  - [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
@@ -14,11 +14,12 @@ Let's start!
14
14
  <a id="install"></a>
15
15
 
16
16
  Install with npm:
17
- ```
17
+ ```sh
18
18
  npm i fastify
19
19
  ```
20
+
20
21
  Install with yarn:
21
- ```
22
+ ```sh
22
23
  yarn add fastify
23
24
  ```
24
25
 
@@ -208,7 +209,7 @@ Let's rewrite the above example with a database connection.
208
209
 
209
210
  First, install `fastify-plugin` and `@fastify/mongodb`:
210
211
 
211
- ```
212
+ ```sh
212
213
  npm i fastify-plugin @fastify/mongodb
213
214
  ```
214
215
 
@@ -550,7 +551,7 @@ Fastify also has CLI integration thanks to
550
551
 
551
552
  First, install `fastify-cli`:
552
553
 
553
- ```
554
+ ```sh
554
555
  npm i fastify-cli
555
556
  ```
556
557
 
@@ -184,25 +184,28 @@ strict mode: use allowUnionTypes to allow union type keyword at "#/properties/im
184
184
  ```
185
185
 
186
186
  As such, schemas like below will need to be changed from:
187
- ```
188
- type: 'object',
189
- properties: {
190
- api_key: { type: 'string' },
191
- image: { type: ['object', 'array'] }
192
- }
187
+ ```js
188
+ {
189
+ type: 'object',
190
+ properties: {
191
+ api_key: { type: 'string' },
192
+ image: { type: ['object', 'array'] }
193
+ }
193
194
  }
194
195
  ```
195
196
 
196
197
  Into:
197
- ```
198
- type: 'object',
199
- properties: {
200
- api_key: { type: 'string' },
201
- image: {
202
- anyOf: [
203
- { type: 'array' },
204
- { type: 'object' }
205
- ]
198
+ ```js
199
+ {
200
+ type: 'object',
201
+ properties: {
202
+ api_key: { type: 'string' },
203
+ image: {
204
+ anyOf: [
205
+ { type: 'array' },
206
+ { type: 'object' }
207
+ ]
208
+ }
206
209
  }
207
210
  }
208
211
  ```
@@ -97,7 +97,6 @@ if (!fastify.hasContentTypeParser('application/jsoff')){
97
97
  }
98
98
  ```
99
99
 
100
- =======
101
100
  #### removeContentTypeParser
102
101
 
103
102
  With `removeContentTypeParser` a single or an array of content types can be
@@ -15,8 +15,8 @@ HTTP2 is supported in all modern browsers __only over a secure connection__:
15
15
  ```js
16
16
  'use strict'
17
17
 
18
- const fs = require('fs')
19
- const path = require('path')
18
+ const fs = require('node:fs')
19
+ const path = require('node:path')
20
20
  const fastify = require('fastify')({
21
21
  http2: true,
22
22
  https: {
@@ -42,8 +42,8 @@ box:
42
42
  ```js
43
43
  'use strict'
44
44
 
45
- const fs = require('fs')
46
- const path = require('path')
45
+ const fs = require('node:fs')
46
+ const path = require('node:path')
47
47
  const fastify = require('fastify')({
48
48
  http2: true,
49
49
  https: {
@@ -24,6 +24,7 @@ are Request/Reply hooks and application hooks:
24
24
  - [Respond to a request from a hook](#respond-to-a-request-from-a-hook)
25
25
  - [Application Hooks](#application-hooks)
26
26
  - [onReady](#onready)
27
+ - [onListen](#onlisten)
27
28
  - [onClose](#onclose)
28
29
  - [preClose](#preclose)
29
30
  - [onRoute](#onroute)
@@ -388,6 +389,7 @@ fastify.addHook('preHandler', async (request, reply) => {
388
389
  You can hook into the application-lifecycle as well.
389
390
 
390
391
  - [onReady](#onready)
392
+ - [onListen](#onlisten)
391
393
  - [onClose](#onclose)
392
394
  - [preClose](#preclose)
393
395
  - [onRoute](#onroute)
@@ -416,6 +418,33 @@ fastify.addHook('onReady', async function () {
416
418
  })
417
419
  ```
418
420
 
421
+ ### onListen
422
+
423
+ Triggered when the server starts listening for requests. The hooks run one
424
+ after another. If a hook function causes an error, it is logged and
425
+ ignored, allowing the queue of hooks to continue. Hook functions accept one
426
+ argument: a callback, `done`, to be invoked after the hook function is
427
+ complete. Hook functions are invoked with `this` bound to the associated
428
+ Fastify instance.
429
+
430
+ This is an alternative to `fastify.server.on('listening', () => {})`.
431
+
432
+ ```js
433
+ // callback style
434
+ fastify.addHook('onListen', function (done) {
435
+ // Some code
436
+ const err = null;
437
+ done(err)
438
+ })
439
+
440
+ // or async/await style
441
+ fastify.addHook('onListen', async function () {
442
+ // Some async code
443
+ })
444
+ ```
445
+
446
+ > **Note**
447
+ > This hook will not run when the server is started using `fastify.inject()` or `fastify.ready()`
419
448
  ### onClose
420
449
  <a id="on-close"></a>
421
450
 
@@ -811,7 +840,7 @@ tools first" fashion.
811
840
 
812
841
  ```js
813
842
  const tracer = /* retrieved from elsewhere in the package */
814
- const dc = require('diagnostics_channel')
843
+ const dc = require('node:diagnostics_channel')
815
844
  const channel = dc.channel('fastify.initialization')
816
845
  const spans = new WeakMap()
817
846
 
@@ -54,7 +54,7 @@ the first parameter to `use` and you are done!
54
54
  `/user/:id/comments`) and wildcards are not supported in multiple paths.*
55
55
 
56
56
  ```js
57
- const path = require('path')
57
+ const path = require('node:path')
58
58
  const serveStatic = require('serve-static')
59
59
 
60
60
  // Single path
@@ -65,7 +65,7 @@ Testing Fastify applications should be a first-class concern.
65
65
 
66
66
  ## Do not monkeypatch core
67
67
 
68
- Moonkeypatch Node.js APIs or installing globals that alter the behavior of the
68
+ Monkeypatch Node.js APIs or installing globals that alter the behavior of the
69
69
  runtime makes building modular applications harder, and limit the use cases of Fastify.
70
70
  Other frameworks do this and we do not.
71
71
 
@@ -242,7 +242,7 @@ reply.trailer('server-timing', function() {
242
242
  return 'db;dur=53, app;dur=47.2'
243
243
  })
244
244
 
245
- const { createHash } = require('crypto')
245
+ const { createHash } = require('node:crypto')
246
246
  // trailer function also receive two argument
247
247
  // @param {object} reply fastify reply
248
248
  // @param {string|Buffer|null} payload payload that already sent, note that it will be null when stream is sent
@@ -668,7 +668,7 @@ fastify.get('/json', options, function (request, reply) {
668
668
  `'application/octet-stream'`.
669
669
  ```js
670
670
  fastify.get('/streams', function (request, reply) {
671
- const fs = require('fs')
671
+ const fs = require('node:fs')
672
672
  const stream = fs.createReadStream('some-file', 'utf8')
673
673
  reply.header('Content-Type', 'application/octet-stream')
674
674
  reply.send(stream)
@@ -677,7 +677,7 @@ fastify.get('/streams', function (request, reply) {
677
677
  When using async-await you will need to return or await the reply object:
678
678
  ```js
679
679
  fastify.get('/streams', async function (request, reply) {
680
- const fs = require('fs')
680
+ const fs = require('node:fs')
681
681
  const stream = fs.createReadStream('some-file', 'utf8')
682
682
  reply.header('Content-Type', 'application/octet-stream')
683
683
  return reply.send(stream)
@@ -690,7 +690,7 @@ fastify.get('/streams', async function (request, reply) {
690
690
  If you are sending a buffer and you have not set a `'Content-Type'` header,
691
691
  *send* will set it to `'application/octet-stream'`.
692
692
  ```js
693
- const fs = require('fs')
693
+ const fs = require('node:fs')
694
694
  fastify.get('/streams', function (request, reply) {
695
695
  fs.readFile('some-file', (err, fileBuffer) => {
696
696
  reply.send(err || fileBuffer)
@@ -700,7 +700,7 @@ fastify.get('/streams', function (request, reply) {
700
700
 
701
701
  When using async-await you will need to return or await the reply object:
702
702
  ```js
703
- const fs = require('fs')
703
+ const fs = require('node:fs')
704
704
  fastify.get('/streams', async function (request, reply) {
705
705
  fs.readFile('some-file', (err, fileBuffer) => {
706
706
  reply.send(err || fileBuffer)
@@ -715,7 +715,7 @@ fastify.get('/streams', async function (request, reply) {
715
715
  `send` manages TypedArray and sets the `'Content-Type'=application/octet-stream'`
716
716
  header if not already set.
717
717
  ```js
718
- const fs = require('fs')
718
+ const fs = require('node:fs')
719
719
  fastify.get('/streams', function (request, reply) {
720
720
  const typedArray = new Uint16Array(10)
721
721
  reply.send(typedArray)
@@ -842,7 +842,7 @@ Fastify natively handles promises and supports async-await.
842
842
 
843
843
  *Note that in the following examples we are not using reply.send.*
844
844
  ```js
845
- const { promisify } = require('util')
845
+ const { promisify } = require('node:util')
846
846
  const delay = promisify(setTimeout)
847
847
 
848
848
  fastify.get('/promises', options, function (request, reply) {
@@ -29,10 +29,10 @@ Request is a core Fastify object containing the following fields:
29
29
  - `url` - the URL of the incoming request
30
30
  - `originalUrl` - similar to `url`, this allows you to access the
31
31
  original `url` in case of internal re-routing
32
- - `routerMethod` - the method defined for the router that is handling the
33
- request
34
- - `routerPath` - the path pattern defined for the router that is handling the
35
- request
32
+ - `routerMethod` - Deprecated, use `request.routeOptions.method` instead. The
33
+ method defined for the router that is handling the request
34
+ - `routerPath` - Deprecated, use `request.routeOptions.config.url` instead. The
35
+ path pattern defined for the router that is handling the request
36
36
  - `is404` - true if request is being handled by 404 handler, false if it is not
37
37
  - `connection` - Deprecated, use `socket` instead. The underlying connection of
38
38
  the incoming request.
@@ -40,17 +40,21 @@ Request is a core Fastify object containing the following fields:
40
40
  - `context` - A Fastify internal object. You should not use it directly or
41
41
  modify it. It is useful to access one special key:
42
42
  - `context.config` - The route [`config`](./Routes.md#routes-config) object.
43
- - `routeSchema` - the scheme definition set for the router that is
44
- handling the request
45
- - `routeConfig` - The route [`config`](./Routes.md#routes-config)
43
+ - `routeSchema` - Deprecated, use `request.routeOptions.schema` instead. The
44
+ scheme definition set for the router that is handling the request
45
+ - `routeConfig` - Deprecated, use `request.routeOptions.config` instead. The
46
+ route [`config`](./Routes.md#routes-config)
46
47
  object.
47
48
  - `routeOptions` - The route [`option`](./Routes.md#routes-options) object
48
49
  - `bodyLimit` - either server limit or route limit
50
+ - `config` - the [`config`](./Routes.md#routes-config) object for this route
49
51
  - `method` - the http method for the route
50
52
  - `url` - the path of the URL to match this route
53
+ - `handler` - the handler for this route
51
54
  - `attachValidation` - attach `validationError` to request
52
55
  (if there is a schema defined)
53
56
  - `logLevel` - log level defined for this route
57
+ - `schema` - the JSON schemas definition for this route
54
58
  - `version` - a semver compatible string that defines the version of the endpoint
55
59
  - `exposeHeadRoute` - creates a sibling HEAD route for any GET routes
56
60
  - `prefixTrailingSlash` - string used to determine how to handle passing /
@@ -635,7 +635,7 @@ You can also use Fastify's default parser but change some handling behavior,
635
635
  like the example below for case insensitive keys and values:
636
636
 
637
637
  ```js
638
- const querystring = require('querystring')
638
+ const querystring = require('node:querystring')
639
639
  const fastify = require('fastify')({
640
640
  querystringParser: str => querystring.parse(str.toLowerCase())
641
641
  })
@@ -1892,7 +1892,7 @@ The properties that can currently be exposed are:
1892
1892
  - http2SessionTimeout
1893
1893
 
1894
1894
  ```js
1895
- const { readFileSync } = require('fs')
1895
+ const { readFileSync } = require('node:fs')
1896
1896
  const Fastify = require('fastify')
1897
1897
 
1898
1898
  const fastify = Fastify({
@@ -32,5 +32,7 @@ fastify
32
32
  })
33
33
 
34
34
  fastify.listen({ port: 3000 }, err => {
35
- if (err) throw err
35
+ if (err) {
36
+ throw err
37
+ }
36
38
  })
@@ -5,7 +5,7 @@ const fastify = require('../../fastify')({
5
5
  })
6
6
 
7
7
  const jsonParser = require('fast-json-body')
8
- const querystring = require('querystring')
8
+ const querystring = require('node:querystring')
9
9
 
10
10
  // Handled by fastify
11
11
  // curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
package/examples/hooks.js CHANGED
@@ -68,6 +68,9 @@ fastify
68
68
  .addHook('onRoute', function (routeOptions) {
69
69
  console.log('onRoute')
70
70
  })
71
+ .addHook('onListen', async function () {
72
+ console.log('onListen')
73
+ })
71
74
  .addHook('onClose', function (instance, done) {
72
75
  console.log('onClose')
73
76
  done()
package/examples/http2.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const fs = require('fs')
4
- const path = require('path')
3
+ const fs = require('node:fs')
4
+ const path = require('node:path')
5
5
  const fastify = require('../fastify')({
6
6
  http2: true,
7
7
  https: {
@@ -33,5 +33,7 @@ fastify
33
33
  })
34
34
 
35
35
  fastify.listen({ port: 3000 }, err => {
36
- if (err) throw err
36
+ if (err) {
37
+ throw err
38
+ }
37
39
  })
package/examples/https.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const fs = require('fs')
4
- const path = require('path')
3
+ const fs = require('node:fs')
4
+ const path = require('node:path')
5
5
  const fastify = require('../fastify')({
6
6
  https: {
7
7
  key: fs.readFileSync(path.join(__dirname, '../test/https/fastify.key')),
@@ -32,5 +32,7 @@ fastify
32
32
  })
33
33
 
34
34
  fastify.listen({ port: 3000 }, err => {
35
- if (err) throw err
35
+ if (err) {
36
+ throw err
37
+ }
36
38
  })
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fastify = require('../fastify')({ logger: true })
4
4
  const jsonParser = require('fast-json-body')
5
- const querystring = require('querystring')
5
+ const querystring = require('node:querystring')
6
6
 
7
7
  // Handled by fastify
8
8
  // curl -X POST -d '{"hello":"world"}' -H'Content-type: application/json' http://localhost:3000/
@@ -47,5 +47,7 @@ fastify
47
47
  })
48
48
 
49
49
  fastify.listen({ port: 3000 }, err => {
50
- if (err) throw err
50
+ if (err) {
51
+ throw err
52
+ }
51
53
  })
@@ -32,5 +32,7 @@ fastify
32
32
  })
33
33
 
34
34
  fastify.listen({ port: 3000 }, err => {
35
- if (err) throw err
35
+ if (err) {
36
+ throw err
37
+ }
36
38
  })
@@ -4,7 +4,7 @@ const fastify = require('../fastify')({
4
4
  logger: false
5
5
  })
6
6
 
7
- const Readable = require('stream').Readable
7
+ const Readable = require('node:stream').Readable
8
8
 
9
9
  fastify
10
10
  .get('/', function (req, reply) {
@@ -13,6 +13,8 @@ fastify
13
13
  })
14
14
 
15
15
  fastify.listen({ port: 3000 }, (err, address) => {
16
- if (err) throw err
16
+ if (err) {
17
+ throw err
18
+ }
17
19
  fastify.log.info(`server listening on ${address}`)
18
20
  })
@@ -26,5 +26,7 @@ fastify
26
26
  })
27
27
 
28
28
  fastify.listen({ port: 3000 }, (err, address) => {
29
- if (err) throw err
29
+ if (err) {
30
+ throw err
31
+ }
30
32
  })
@@ -75,4 +75,5 @@ server.listen({ port: 8080 }, (err, address) => {
75
75
  console.error(err);
76
76
  process.exit(1);
77
77
  }
78
+ console.log(`server listening on ${address}`)
78
79
  });
@@ -15,7 +15,9 @@ const opts = {
15
15
  }
16
16
  }
17
17
  fastify.register(require('./plugin'), opts, function (err) {
18
- if (err) throw err
18
+ if (err) {
19
+ throw err
20
+ }
19
21
  })
20
22
 
21
23
  fastify.listen({ port: 3000 }, function (err) {