node-fastify 5.8.3
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.
- package/.borp.yaml +3 -0
- package/.markdownlint-cli2.yaml +22 -0
- package/.prettierignore +1 -0
- package/GOVERNANCE.md +4 -0
- package/LICENSE +21 -0
- package/PROJECT_CHARTER.md +126 -0
- package/README.md +423 -0
- package/SECURITY.md +220 -0
- package/SPONSORS.md +24 -0
- package/build/build-error-serializer.js +35 -0
- package/build/build-validation.js +169 -0
- package/build/sync-version.js +11 -0
- package/docs/Guides/Benchmarking.md +60 -0
- package/docs/Guides/Database.md +321 -0
- package/docs/Guides/Delay-Accepting-Requests.md +608 -0
- package/docs/Guides/Detecting-When-Clients-Abort.md +172 -0
- package/docs/Guides/Ecosystem.md +726 -0
- package/docs/Guides/Fluent-Schema.md +127 -0
- package/docs/Guides/Getting-Started.md +620 -0
- package/docs/Guides/Index.md +43 -0
- package/docs/Guides/Migration-Guide-V3.md +287 -0
- package/docs/Guides/Migration-Guide-V4.md +267 -0
- package/docs/Guides/Migration-Guide-V5.md +727 -0
- package/docs/Guides/Plugins-Guide.md +520 -0
- package/docs/Guides/Prototype-Poisoning.md +383 -0
- package/docs/Guides/Recommendations.md +378 -0
- package/docs/Guides/Serverless.md +604 -0
- package/docs/Guides/Style-Guide.md +246 -0
- package/docs/Guides/Testing.md +481 -0
- package/docs/Guides/Write-Plugin.md +103 -0
- package/docs/Guides/Write-Type-Provider.md +34 -0
- package/docs/Reference/ContentTypeParser.md +271 -0
- package/docs/Reference/Decorators.md +436 -0
- package/docs/Reference/Encapsulation.md +194 -0
- package/docs/Reference/Errors.md +377 -0
- package/docs/Reference/HTTP2.md +94 -0
- package/docs/Reference/Hooks.md +958 -0
- package/docs/Reference/Index.md +73 -0
- package/docs/Reference/LTS.md +86 -0
- package/docs/Reference/Lifecycle.md +99 -0
- package/docs/Reference/Logging.md +268 -0
- package/docs/Reference/Middleware.md +79 -0
- package/docs/Reference/Plugins.md +245 -0
- package/docs/Reference/Principles.md +73 -0
- package/docs/Reference/Reply.md +1001 -0
- package/docs/Reference/Request.md +295 -0
- package/docs/Reference/Routes.md +802 -0
- package/docs/Reference/Server.md +2389 -0
- package/docs/Reference/Type-Providers.md +256 -0
- package/docs/Reference/TypeScript.md +1729 -0
- package/docs/Reference/Validation-and-Serialization.md +1130 -0
- package/docs/Reference/Warnings.md +58 -0
- package/docs/index.md +24 -0
- package/docs/resources/encapsulation_context.drawio +1 -0
- package/docs/resources/encapsulation_context.svg +3 -0
- package/eslint.config.js +35 -0
- package/examples/asyncawait.js +38 -0
- package/examples/benchmark/body.json +3 -0
- package/examples/benchmark/hooks-benchmark-async-await.js +44 -0
- package/examples/benchmark/hooks-benchmark.js +52 -0
- package/examples/benchmark/parser.js +47 -0
- package/examples/benchmark/simple.js +30 -0
- package/examples/benchmark/webstream.js +27 -0
- package/examples/hooks.js +91 -0
- package/examples/http2.js +39 -0
- package/examples/https.js +38 -0
- package/examples/parser.js +53 -0
- package/examples/plugin.js +12 -0
- package/examples/route-prefix.js +38 -0
- package/examples/shared-schema.js +38 -0
- package/examples/simple-stream.js +20 -0
- package/examples/simple.js +32 -0
- package/examples/simple.mjs +27 -0
- package/examples/typescript-server.ts +79 -0
- package/examples/use-plugin.js +29 -0
- package/fastify.d.ts +253 -0
- package/fastify.js +985 -0
- package/integration/server.js +29 -0
- package/integration/test.sh +23 -0
- package/lib/config-validator.js +1266 -0
- package/lib/content-type-parser.js +413 -0
- package/lib/content-type.js +160 -0
- package/lib/context.js +98 -0
- package/lib/decorate.js +152 -0
- package/lib/error-handler.js +173 -0
- package/lib/error-serializer.js +134 -0
- package/lib/error-status.js +14 -0
- package/lib/errors.js +516 -0
- package/lib/four-oh-four.js +190 -0
- package/lib/handle-request.js +195 -0
- package/lib/head-route.js +45 -0
- package/lib/hooks.js +429 -0
- package/lib/initial-config-validation.js +37 -0
- package/lib/logger-factory.js +136 -0
- package/lib/logger-pino.js +68 -0
- package/lib/noop-set.js +10 -0
- package/lib/plugin-override.js +90 -0
- package/lib/plugin-utils.js +169 -0
- package/lib/promise.js +23 -0
- package/lib/reply.js +1030 -0
- package/lib/req-id-gen-factory.js +52 -0
- package/lib/request.js +391 -0
- package/lib/route.js +686 -0
- package/lib/schema-controller.js +164 -0
- package/lib/schemas.js +207 -0
- package/lib/server.js +441 -0
- package/lib/symbols.js +71 -0
- package/lib/validation.js +280 -0
- package/lib/warnings.js +57 -0
- package/lib/wrap-thenable.js +84 -0
- package/package.json +225 -0
- package/scripts/validate-ecosystem-links.js +179 -0
- package/test/404s.test.js +2035 -0
- package/test/500s.test.js +422 -0
- package/test/allow-unsafe-regex.test.js +92 -0
- package/test/als.test.js +65 -0
- package/test/async-await.test.js +705 -0
- package/test/async-dispose.test.js +20 -0
- package/test/async_hooks.test.js +52 -0
- package/test/body-limit.test.js +224 -0
- package/test/buffer.test.js +74 -0
- package/test/build/error-serializer.test.js +36 -0
- package/test/build/version.test.js +14 -0
- package/test/build-certificate.js +109 -0
- package/test/bundler/README.md +29 -0
- package/test/bundler/esbuild/bundler-test.js +32 -0
- package/test/bundler/esbuild/package.json +10 -0
- package/test/bundler/esbuild/src/fail-plugin-version.js +14 -0
- package/test/bundler/esbuild/src/index.js +9 -0
- package/test/bundler/webpack/bundler-test.js +32 -0
- package/test/bundler/webpack/package.json +11 -0
- package/test/bundler/webpack/src/fail-plugin-version.js +14 -0
- package/test/bundler/webpack/src/index.js +9 -0
- package/test/bundler/webpack/webpack.config.js +15 -0
- package/test/case-insensitive.test.js +102 -0
- package/test/chainable.test.js +40 -0
- package/test/child-logger-factory.test.js +128 -0
- package/test/client-timeout.test.js +38 -0
- package/test/close-pipelining.test.js +78 -0
- package/test/close.test.js +706 -0
- package/test/conditional-pino.test.js +47 -0
- package/test/connection-timeout.test.js +42 -0
- package/test/constrained-routes.test.js +1138 -0
- package/test/content-length.test.js +174 -0
- package/test/content-parser.test.js +739 -0
- package/test/content-type.test.js +181 -0
- package/test/context-config.test.js +164 -0
- package/test/custom-http-server.test.js +118 -0
- package/test/custom-parser-async.test.js +59 -0
- package/test/custom-parser.0.test.js +701 -0
- package/test/custom-parser.1.test.js +266 -0
- package/test/custom-parser.2.test.js +91 -0
- package/test/custom-parser.3.test.js +208 -0
- package/test/custom-parser.4.test.js +218 -0
- package/test/custom-parser.5.test.js +130 -0
- package/test/custom-querystring-parser.test.js +129 -0
- package/test/decorator.test.js +1330 -0
- package/test/delete.test.js +344 -0
- package/test/diagnostics-channel/404.test.js +49 -0
- package/test/diagnostics-channel/async-delay-request.test.js +65 -0
- package/test/diagnostics-channel/async-request.test.js +64 -0
- package/test/diagnostics-channel/error-before-handler.test.js +35 -0
- package/test/diagnostics-channel/error-request.test.js +53 -0
- package/test/diagnostics-channel/error-status.test.js +123 -0
- package/test/diagnostics-channel/init.test.js +50 -0
- package/test/diagnostics-channel/sync-delay-request.test.js +49 -0
- package/test/diagnostics-channel/sync-request-reply.test.js +51 -0
- package/test/diagnostics-channel/sync-request.test.js +54 -0
- package/test/encapsulated-child-logger-factory.test.js +69 -0
- package/test/encapsulated-error-handler.test.js +237 -0
- package/test/esm/errorCodes.test.mjs +10 -0
- package/test/esm/esm.test.mjs +13 -0
- package/test/esm/index.test.js +8 -0
- package/test/esm/named-exports.mjs +14 -0
- package/test/esm/other.mjs +8 -0
- package/test/esm/plugin.mjs +8 -0
- package/test/fastify-instance.test.js +300 -0
- package/test/find-route.test.js +152 -0
- package/test/fluent-schema.test.js +209 -0
- package/test/genReqId.test.js +426 -0
- package/test/handler-context.test.js +45 -0
- package/test/handler-timeout.test.js +367 -0
- package/test/has-route.test.js +88 -0
- package/test/header-overflow.test.js +55 -0
- package/test/helper.js +496 -0
- package/test/hooks-async.test.js +1099 -0
- package/test/hooks.on-listen.test.js +1162 -0
- package/test/hooks.on-ready.test.js +421 -0
- package/test/hooks.test.js +3578 -0
- package/test/http-methods/copy.test.js +35 -0
- package/test/http-methods/custom-http-methods.test.js +114 -0
- package/test/http-methods/get.test.js +412 -0
- package/test/http-methods/head.test.js +263 -0
- package/test/http-methods/lock.test.js +108 -0
- package/test/http-methods/mkcalendar.test.js +143 -0
- package/test/http-methods/mkcol.test.js +35 -0
- package/test/http-methods/move.test.js +42 -0
- package/test/http-methods/propfind.test.js +136 -0
- package/test/http-methods/proppatch.test.js +105 -0
- package/test/http-methods/report.test.js +142 -0
- package/test/http-methods/search.test.js +233 -0
- package/test/http-methods/trace.test.js +21 -0
- package/test/http-methods/unlock.test.js +38 -0
- package/test/http2/closing.test.js +270 -0
- package/test/http2/constraint.test.js +109 -0
- package/test/http2/head.test.js +34 -0
- package/test/http2/plain.test.js +68 -0
- package/test/http2/secure-with-fallback.test.js +113 -0
- package/test/http2/secure.test.js +67 -0
- package/test/http2/unknown-http-method.test.js +34 -0
- package/test/https/custom-https-server.test.js +58 -0
- package/test/https/https.test.js +136 -0
- package/test/imports.test.js +17 -0
- package/test/inject.test.js +502 -0
- package/test/input-validation.js +335 -0
- package/test/internals/all.test.js +38 -0
- package/test/internals/content-type-parser.test.js +111 -0
- package/test/internals/context.test.js +31 -0
- package/test/internals/decorator.test.js +156 -0
- package/test/internals/errors.test.js +982 -0
- package/test/internals/handle-request.test.js +270 -0
- package/test/internals/hook-runner.test.js +449 -0
- package/test/internals/hooks.test.js +96 -0
- package/test/internals/initial-config.test.js +383 -0
- package/test/internals/logger.test.js +163 -0
- package/test/internals/plugin.test.js +170 -0
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply-serialize.test.js +714 -0
- package/test/internals/reply.test.js +1920 -0
- package/test/internals/req-id-gen-factory.test.js +133 -0
- package/test/internals/request-validate.test.js +1402 -0
- package/test/internals/request.test.js +506 -0
- package/test/internals/schema-controller-perf.test.js +40 -0
- package/test/internals/server.test.js +91 -0
- package/test/internals/validation.test.js +352 -0
- package/test/issue-4959.test.js +118 -0
- package/test/keep-alive-timeout.test.js +42 -0
- package/test/listen.1.test.js +154 -0
- package/test/listen.2.test.js +113 -0
- package/test/listen.3.test.js +83 -0
- package/test/listen.4.test.js +168 -0
- package/test/listen.5.test.js +122 -0
- package/test/logger/instantiation.test.js +341 -0
- package/test/logger/logger-test-utils.js +47 -0
- package/test/logger/logging.test.js +460 -0
- package/test/logger/options.test.js +579 -0
- package/test/logger/request.test.js +292 -0
- package/test/logger/response.test.js +183 -0
- package/test/logger/tap-parallel-not-ok +0 -0
- package/test/max-requests-per-socket.test.js +113 -0
- package/test/middleware.test.js +37 -0
- package/test/noop-set.test.js +19 -0
- package/test/nullable-validation.test.js +187 -0
- package/test/options.error-handler.test.js +5 -0
- package/test/options.test.js +5 -0
- package/test/output-validation.test.js +140 -0
- package/test/patch.error-handler.test.js +5 -0
- package/test/patch.test.js +5 -0
- package/test/plugin.1.test.js +230 -0
- package/test/plugin.2.test.js +314 -0
- package/test/plugin.3.test.js +287 -0
- package/test/plugin.4.test.js +504 -0
- package/test/plugin.helper.js +8 -0
- package/test/plugin.name.display.js +10 -0
- package/test/post-empty-body.test.js +38 -0
- package/test/pretty-print.test.js +366 -0
- package/test/promises.test.js +125 -0
- package/test/proto-poisoning.test.js +145 -0
- package/test/put.error-handler.test.js +5 -0
- package/test/put.test.js +5 -0
- package/test/register.test.js +184 -0
- package/test/reply-code.test.js +148 -0
- package/test/reply-early-hints.test.js +100 -0
- package/test/reply-error.test.js +815 -0
- package/test/reply-trailers.test.js +445 -0
- package/test/reply-web-stream-locked.test.js +37 -0
- package/test/request-error.test.js +624 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +118 -0
- package/test/request-timeout.test.js +53 -0
- package/test/route-hooks.test.js +635 -0
- package/test/route-prefix.test.js +904 -0
- package/test/route-shorthand.test.js +48 -0
- package/test/route.1.test.js +259 -0
- package/test/route.2.test.js +100 -0
- package/test/route.3.test.js +213 -0
- package/test/route.4.test.js +127 -0
- package/test/route.5.test.js +211 -0
- package/test/route.6.test.js +306 -0
- package/test/route.7.test.js +406 -0
- package/test/route.8.test.js +225 -0
- package/test/router-options.test.js +1108 -0
- package/test/same-shape.test.js +124 -0
- package/test/schema-examples.test.js +661 -0
- package/test/schema-feature.test.js +2198 -0
- package/test/schema-serialization.test.js +1171 -0
- package/test/schema-special-usage.test.js +1348 -0
- package/test/schema-validation.test.js +1572 -0
- package/test/scripts/validate-ecosystem-links.test.js +339 -0
- package/test/serialize-response.test.js +186 -0
- package/test/server.test.js +347 -0
- package/test/set-error-handler.test.js +69 -0
- package/test/skip-reply-send.test.js +317 -0
- package/test/stream-serializers.test.js +40 -0
- package/test/stream.1.test.js +94 -0
- package/test/stream.2.test.js +129 -0
- package/test/stream.3.test.js +198 -0
- package/test/stream.4.test.js +176 -0
- package/test/stream.5.test.js +188 -0
- package/test/sync-routes.test.js +32 -0
- package/test/throw.test.js +359 -0
- package/test/toolkit.js +63 -0
- package/test/trust-proxy.test.js +162 -0
- package/test/type-provider.test.js +22 -0
- package/test/types/content-type-parser.test-d.ts +72 -0
- package/test/types/decorate-request-reply.test-d.ts +18 -0
- package/test/types/dummy-plugin.ts +9 -0
- package/test/types/errors.test-d.ts +90 -0
- package/test/types/fastify.test-d.ts +352 -0
- package/test/types/hooks.test-d.ts +550 -0
- package/test/types/import.ts +2 -0
- package/test/types/instance.test-d.ts +588 -0
- package/test/types/logger.test-d.ts +277 -0
- package/test/types/plugin.test-d.ts +97 -0
- package/test/types/register.test-d.ts +237 -0
- package/test/types/reply.test-d.ts +254 -0
- package/test/types/request.test-d.ts +188 -0
- package/test/types/route.test-d.ts +553 -0
- package/test/types/schema.test-d.ts +135 -0
- package/test/types/serverFactory.test-d.ts +37 -0
- package/test/types/type-provider.test-d.ts +1213 -0
- package/test/types/using.test-d.ts +17 -0
- package/test/upgrade.test.js +52 -0
- package/test/url-rewriting.test.js +122 -0
- package/test/use-semicolon-delimiter.test.js +168 -0
- package/test/validation-error-handling.test.js +900 -0
- package/test/versioned-routes.test.js +603 -0
- package/test/web-api.test.js +616 -0
- package/test/wrap-thenable.test.js +30 -0
- package/types/content-type-parser.d.ts +75 -0
- package/types/context.d.ts +22 -0
- package/types/errors.d.ts +92 -0
- package/types/hooks.d.ts +875 -0
- package/types/instance.d.ts +609 -0
- package/types/logger.d.ts +107 -0
- package/types/plugin.d.ts +44 -0
- package/types/register.d.ts +42 -0
- package/types/reply.d.ts +81 -0
- package/types/request.d.ts +95 -0
- package/types/route.d.ts +199 -0
- package/types/schema.d.ts +61 -0
- package/types/server-factory.d.ts +19 -0
- package/types/type-provider.d.ts +130 -0
- package/types/utils.d.ts +98 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
## Core Documents
|
|
4
|
+
<a id="reference-core-docs"></a>
|
|
5
|
+
|
|
6
|
+
For the full table of contents (TOC), see [below](#reference-toc). The following
|
|
7
|
+
list is a subset of the full TOC that detail core Fastify APIs and concepts in
|
|
8
|
+
order of most likely importance to the reader:
|
|
9
|
+
|
|
10
|
+
+ [Server](./Server.md): Documents the core Fastify API. Includes documentation
|
|
11
|
+
for the factory function and the object returned by the factory function.
|
|
12
|
+
+ [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
|
|
13
|
+
illustrates where [Hooks](./Hooks.md) are available for integrating with it.
|
|
14
|
+
+ [Routes](./Routes.md): Details how to register routes with Fastify and how
|
|
15
|
+
Fastify builds and evaluates the routing trie.
|
|
16
|
+
+ [Request](./Request.md): Details Fastify's request object that is passed into
|
|
17
|
+
each request handler.
|
|
18
|
+
+ [Reply](./Reply.md): Details Fastify's response object available to each
|
|
19
|
+
request handler.
|
|
20
|
+
+ [Validation and Serialization](./Validation-and-Serialization.md): Details
|
|
21
|
+
Fastify's support for validating incoming data and how Fastify serializes data
|
|
22
|
+
for responses.
|
|
23
|
+
+ [Plugins](./Plugins.md): Explains Fastify's plugin architecture and API.
|
|
24
|
+
+ [Encapsulation](./Encapsulation.md): Explains a core concept upon which all
|
|
25
|
+
Fastify plugins are built.
|
|
26
|
+
+ [Decorators](./Decorators.md): Explains the server, request, and response
|
|
27
|
+
decorator APIs.
|
|
28
|
+
+ [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
|
|
29
|
+
themselves into Fastify's handling of the request lifecycle.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Reference Documentation Table Of Contents
|
|
33
|
+
<a id="reference-toc"></a>
|
|
34
|
+
|
|
35
|
+
This table of contents is in alphabetical order.
|
|
36
|
+
|
|
37
|
+
+ [Content Type Parser](./ContentTypeParser.md): Documents Fastify's default
|
|
38
|
+
content type parser and how to add support for new content types.
|
|
39
|
+
+ [Decorators](./Decorators.md): Explains the server, request, and response
|
|
40
|
+
decorator APIs.
|
|
41
|
+
+ [Encapsulation](./Encapsulation.md): Explains a core concept upon which all
|
|
42
|
+
Fastify plugins are built.
|
|
43
|
+
+ [Errors](./Errors.md): Details how Fastify handles errors and lists the
|
|
44
|
+
standard set of errors Fastify generates.
|
|
45
|
+
+ [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
|
|
46
|
+
themselves into Fastify's handling of the request lifecycle.
|
|
47
|
+
+ [HTTP2](./HTTP2.md): Details Fastify's HTTP2 support.
|
|
48
|
+
+ [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
|
|
49
|
+
illustrates where [Hooks](./Hooks.md) are available for integrating with it.
|
|
50
|
+
+ [Logging](./Logging.md): Details Fastify's included logging and how to
|
|
51
|
+
customize it.
|
|
52
|
+
+ [Long Term Support](./LTS.md): Explains Fastify's long term support (LTS)
|
|
53
|
+
guarantee and the exceptions possible to the [semver](https://semver.org)
|
|
54
|
+
contract.
|
|
55
|
+
+ [Middleware](./Middleware.md): Details Fastify's support for Express.js style
|
|
56
|
+
middleware.
|
|
57
|
+
+ [Plugins](./Plugins.md): Explains Fastify's plugin architecture and API.
|
|
58
|
+
+ [Reply](./Reply.md): Details Fastify's response object available to each
|
|
59
|
+
request handler.
|
|
60
|
+
+ [Request](./Request.md): Details Fastify's request object that is passed into
|
|
61
|
+
each request handler.
|
|
62
|
+
+ [Routes](./Routes.md): Details how to register routes with Fastify and how
|
|
63
|
+
Fastify builds and evaluates the routing trie.
|
|
64
|
+
+ [Server](./Server.md): Documents the core Fastify API. Includes documentation
|
|
65
|
+
for the factory function and the object returned by the factory function.
|
|
66
|
+
+ [TypeScript](./TypeScript.md): Documents Fastify's TypeScript support and
|
|
67
|
+
provides recommendations for writing applications in TypeScript that utilize
|
|
68
|
+
Fastify.
|
|
69
|
+
+ [Validation and Serialization](./Validation-and-Serialization.md): Details
|
|
70
|
+
Fastify's support for validating incoming data and how Fastify serializes data
|
|
71
|
+
for responses.
|
|
72
|
+
+ [Warnings](./Warnings.md): Details the warnings Fastify emits and how to
|
|
73
|
+
solve them.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
## Long Term Support
|
|
4
|
+
<a id="lts"></a>
|
|
5
|
+
|
|
6
|
+
Fastify's Long Term Support (LTS) is provided according to the schedule laid out
|
|
7
|
+
in this document:
|
|
8
|
+
|
|
9
|
+
1. Major releases, "X" release of [semantic versioning][semver] X.Y.Z release
|
|
10
|
+
versions, are supported for a minimum period of six months from their release
|
|
11
|
+
date. The release date of any specific version can be found at
|
|
12
|
+
[https://github.com/fastify/fastify/releases](https://github.com/fastify/fastify/releases).
|
|
13
|
+
2. Major releases will receive security updates for an additional six months
|
|
14
|
+
from the release of the next major release. After this period we will still
|
|
15
|
+
review and release security fixes as long as they are provided by the
|
|
16
|
+
community and they do not violate other constraints, e.g. minimum supported
|
|
17
|
+
Node.js version.
|
|
18
|
+
3. Major releases will be tested and verified against all Node.js release lines
|
|
19
|
+
that are supported by the [Node.js LTS
|
|
20
|
+
policy](https://github.com/nodejs/Release) within the LTS period of that
|
|
21
|
+
given Fastify release line. This implies that only the latest Node.js release
|
|
22
|
+
of a given line is supported.
|
|
23
|
+
4. In addition to Node.js runtime, major releases of Fastify will also be tested
|
|
24
|
+
and verified against alternative runtimes that are compatible with Node.js.
|
|
25
|
+
The maintenance teams of these alternative runtimes are responsible for ensuring
|
|
26
|
+
and guaranteeing these tests work properly.
|
|
27
|
+
1. [N|Solid](https://docs.nodesource.com/docs/product_suite) tests and
|
|
28
|
+
verifies each Fastify major release against current N|Solid LTS versions.
|
|
29
|
+
NodeSource ensures Fastify compatibility with N|Solid, aligning with the
|
|
30
|
+
support scope of N|Solid LTS versions at the time of the Fastify release.
|
|
31
|
+
This guarantees N|Solid users can confidently use Fastify.
|
|
32
|
+
|
|
33
|
+
A "month" is defined as 30 consecutive days.
|
|
34
|
+
|
|
35
|
+
> ## Security Releases and Semver
|
|
36
|
+
>
|
|
37
|
+
> As a consequence of providing long-term support for major releases, there are
|
|
38
|
+
> occasions where we need to release breaking changes as a _minor_ version
|
|
39
|
+
> release. Such changes will _always_ be noted in the [release
|
|
40
|
+
> notes](https://github.com/fastify/fastify/releases).
|
|
41
|
+
>
|
|
42
|
+
> To avoid automatically receiving breaking security updates it is possible to
|
|
43
|
+
> use the tilde (`~`) range qualifier. For example, to get patches for the 3.15
|
|
44
|
+
> release, and avoid automatically updating to the 3.16 release, specify the
|
|
45
|
+
> dependency as `"fastify": "~3.15.x"`. This will leave your application
|
|
46
|
+
> vulnerable, so please use it with caution.
|
|
47
|
+
|
|
48
|
+
### Security Support Beyond LTS
|
|
49
|
+
|
|
50
|
+
Fastify's partner, HeroDevs, provides commercial security support through the
|
|
51
|
+
OpenJS Ecosystem Sustainability Program for versions of Fastify that are EOL.
|
|
52
|
+
For more information, see their [Never Ending Support][hd-link] service.
|
|
53
|
+
|
|
54
|
+
### Schedule
|
|
55
|
+
<a id="lts-schedule"></a>
|
|
56
|
+
|
|
57
|
+
| Version | Release Date | End Of LTS Date | Node.js | Nsolid(Node) |
|
|
58
|
+
| :------ | :----------- | :-------------- | :----------------- | :------------- |
|
|
59
|
+
| 1.0.0 | 2018-03-06 | 2019-09-01 | 6, 8, 9, 10, 11 | |
|
|
60
|
+
| 2.0.0 | 2019-02-25 | 2021-01-31 | 6, 8, 10, 12, 14 | |
|
|
61
|
+
| 3.0.0 | 2020-07-07 | 2023-06-30 | 10, 12, 14, 16, 18 | v5(18) |
|
|
62
|
+
| 4.0.0 | 2022-06-08 | 2025-06-30 | 14, 16, 18, 20, 22 | v5(18), v5(20) |
|
|
63
|
+
| 5.0.0 | 2024-09-17 | TBD | 20, 22 | v5(20) |
|
|
64
|
+
|
|
65
|
+
### CI tested operating systems
|
|
66
|
+
<a id="supported-os"></a>
|
|
67
|
+
|
|
68
|
+
Fastify uses GitHub Actions for CI testing, please refer to [GitHub's
|
|
69
|
+
documentation regarding workflow
|
|
70
|
+
runners](https://docs.github.com/en/actions/reference/runners/github-hosted-runners#supported-runners-and-hardware-resources)
|
|
71
|
+
for further details on what the latest virtual environment is in relation to the
|
|
72
|
+
YAML workflow labels below:
|
|
73
|
+
|
|
74
|
+
| OS | YAML Workflow Label | Package Manager | Node.js | Nsolid(Node) |
|
|
75
|
+
| ------- | ------------------- | --------------- | ----------- | ------------- |
|
|
76
|
+
| Linux | `ubuntu-latest` | npm | 20 | v5(20) |
|
|
77
|
+
| Linux | `ubuntu-latest` | yarn,pnpm | 20 | v5(20) |
|
|
78
|
+
| Windows | `windows-latest` | npm | 20 | v5(20) |
|
|
79
|
+
| MacOS | `macos-latest` | npm | 20 | v5(20) |
|
|
80
|
+
|
|
81
|
+
Using [yarn](https://yarnpkg.com/) might require passing the `--ignore-engines`
|
|
82
|
+
flag.
|
|
83
|
+
|
|
84
|
+
[semver]: https://semver.org/
|
|
85
|
+
|
|
86
|
+
[hd-link]: https://www.herodevs.com/support/fastify-nes?utm_source=fastify&utm_medium=link&utm_campaign=eol_support_fastify
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
## Lifecycle
|
|
4
|
+
<a id="lifecycle"></a>
|
|
5
|
+
|
|
6
|
+
This schema shows the internal lifecycle of Fastify.
|
|
7
|
+
|
|
8
|
+
The right branch of each section shows the next phase of the lifecycle. The left
|
|
9
|
+
branch shows the corresponding error code generated if the parent throws an
|
|
10
|
+
error. All errors are automatically handled by Fastify.
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
Incoming Request
|
|
14
|
+
│
|
|
15
|
+
└─▶ Routing
|
|
16
|
+
│
|
|
17
|
+
└─▶ Instance Logger
|
|
18
|
+
│
|
|
19
|
+
4**/5** ◀─┴─▶ onRequest Hook
|
|
20
|
+
│
|
|
21
|
+
4**/5** ◀─┴─▶ preParsing Hook
|
|
22
|
+
│
|
|
23
|
+
4**/5** ◀─┴─▶ Parsing
|
|
24
|
+
│
|
|
25
|
+
4**/5** ◀─┴─▶ preValidation Hook
|
|
26
|
+
│
|
|
27
|
+
400 ◀─┴─▶ Validation
|
|
28
|
+
│
|
|
29
|
+
4**/5** ◀─┴─▶ preHandler Hook
|
|
30
|
+
│
|
|
31
|
+
4**/5** ◀─┴─▶ User Handler
|
|
32
|
+
│
|
|
33
|
+
└─▶ Reply
|
|
34
|
+
│
|
|
35
|
+
4**/5** ◀─┴─▶ preSerialization Hook
|
|
36
|
+
│
|
|
37
|
+
└─▶ onSend Hook
|
|
38
|
+
│
|
|
39
|
+
4**/5** ◀─┴─▶ Outgoing Response
|
|
40
|
+
│
|
|
41
|
+
└─▶ onResponse Hook
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
When `handlerTimeout` is configured, a timer starts after routing. If the
|
|
45
|
+
response is not sent within the allowed time, `request.signal` is aborted and
|
|
46
|
+
a 503 error is sent. The timer is cleared when the response finishes or when
|
|
47
|
+
`reply.hijack()` is called. See [`handlerTimeout`](./Server.md#factory-handler-timeout).
|
|
48
|
+
|
|
49
|
+
Before or during the `User Handler`, `reply.hijack()` can be called to:
|
|
50
|
+
- Prevent Fastify from running subsequent hooks and the user handler
|
|
51
|
+
- Prevent Fastify from sending the response automatically
|
|
52
|
+
|
|
53
|
+
If `reply.raw` is used to send a response, `onResponse` hooks will still
|
|
54
|
+
be executed.
|
|
55
|
+
|
|
56
|
+
## Reply Lifecycle
|
|
57
|
+
<a id="reply-lifecycle"></a>
|
|
58
|
+
|
|
59
|
+
When the user handles the request, the result may be:
|
|
60
|
+
|
|
61
|
+
- In an async handler: it returns a payload or throws an `Error`
|
|
62
|
+
- In a sync handler: it sends a payload or an `Error` instance
|
|
63
|
+
|
|
64
|
+
If the reply was hijacked, all subsequent steps are skipped. Otherwise, when
|
|
65
|
+
submitted, the data flow is as follows:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
★ schema validation Error
|
|
69
|
+
│
|
|
70
|
+
└─▶ schemaErrorFormatter
|
|
71
|
+
│
|
|
72
|
+
reply sent ◀── JSON ─┴─ Error instance
|
|
73
|
+
│
|
|
74
|
+
│ ★ throw an Error
|
|
75
|
+
★ send or return │ │
|
|
76
|
+
│ │ │
|
|
77
|
+
│ ▼ │
|
|
78
|
+
reply sent ◀── JSON ─┴─ Error instance ──▶ onError Hook ◀───────┘
|
|
79
|
+
│
|
|
80
|
+
reply sent ◀── JSON ─┴─ Error instance ──▶ setErrorHandler
|
|
81
|
+
│
|
|
82
|
+
└─▶ reply sent
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`reply sent` means the JSON payload will be serialized by one of the following:
|
|
86
|
+
- The [reply serializer](./Server.md#setreplyserializer) if set
|
|
87
|
+
- The [serializer compiler](./Server.md#setserializercompiler) if a JSON schema
|
|
88
|
+
is set for the HTTP status code
|
|
89
|
+
- The default `JSON.stringify` function
|
|
90
|
+
|
|
91
|
+
## Shutdown Lifecycle
|
|
92
|
+
<a id="shutdown-lifecycle"></a>
|
|
93
|
+
|
|
94
|
+
When [`fastify.close()`](./Server.md#close) is called, the server goes through a
|
|
95
|
+
graceful shutdown sequence involving
|
|
96
|
+
[`preClose`](./Hooks.md#pre-close) hooks, connection draining, and
|
|
97
|
+
[`onClose`](./Hooks.md#on-close) hooks. See the
|
|
98
|
+
[`close`](./Server.md#close) method documentation for the full step-by-step
|
|
99
|
+
lifecycle.
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
## Logging
|
|
4
|
+
|
|
5
|
+
### Enable Logging
|
|
6
|
+
Logging is disabled by default. Enable it by passing `{ logger: true }` or
|
|
7
|
+
`{ logger: { level: 'info' } }` when creating a Fastify instance. Note that if
|
|
8
|
+
the logger is disabled, it cannot be enabled at runtime.
|
|
9
|
+
[abstract-logging](https://www.npmjs.com/package/abstract-logging) is used for
|
|
10
|
+
this purpose.
|
|
11
|
+
|
|
12
|
+
As Fastify is focused on performance, it uses
|
|
13
|
+
[pino](https://github.com/pinojs/pino) as its logger, with the default log
|
|
14
|
+
level set to `'info'` when enabled.
|
|
15
|
+
|
|
16
|
+
#### Basic logging setup
|
|
17
|
+
Enabling the production JSON logger:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const fastify = require('fastify')({
|
|
21
|
+
logger: true
|
|
22
|
+
})
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
#### Environment-Specific Configuration
|
|
26
|
+
Enabling the logger with appropriate configuration for local development,
|
|
27
|
+
production, and test environments requires more configuration:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const envToLogger = {
|
|
31
|
+
development: {
|
|
32
|
+
transport: {
|
|
33
|
+
target: 'pino-pretty',
|
|
34
|
+
options: {
|
|
35
|
+
translateTime: 'HH:MM:ss Z',
|
|
36
|
+
ignore: 'pid,hostname',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
production: true,
|
|
41
|
+
test: false,
|
|
42
|
+
}
|
|
43
|
+
const fastify = require('fastify')({
|
|
44
|
+
logger: envToLogger[environment] ?? true // defaults to true if no entry matches in the map
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
⚠️ `pino-pretty` needs to be installed as a dev dependency. It is not included
|
|
48
|
+
by default for performance reasons.
|
|
49
|
+
|
|
50
|
+
### Usage
|
|
51
|
+
The logger can be used in route handlers as follows:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
fastify.get('/', options, function (request, reply) {
|
|
55
|
+
request.log.info('Some info about the current request')
|
|
56
|
+
reply.send({ hello: 'world' })
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Trigger new logs outside route handlers using the Pino instance from the Fastify
|
|
61
|
+
instance:
|
|
62
|
+
```js
|
|
63
|
+
fastify.log.info('Something important happened!');
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Passing Logger Options
|
|
67
|
+
To pass options to the logger, provide them to Fastify. See the
|
|
68
|
+
[Pino documentation](https://github.com/pinojs/pino/blob/main/docs/api.md#options)
|
|
69
|
+
for available options. To specify a file destination, use:
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
const fastify = require('fastify')({
|
|
73
|
+
logger: {
|
|
74
|
+
level: 'info',
|
|
75
|
+
file: '/path/to/file' // Will use pino.destination()
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
fastify.get('/', options, function (request, reply) {
|
|
80
|
+
request.log.info('Some info about the current request')
|
|
81
|
+
reply.send({ hello: 'world' })
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
To pass a custom stream to the Pino instance, add a `stream` field to the logger
|
|
86
|
+
object:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
const split = require('split2')
|
|
90
|
+
const stream = split(JSON.parse)
|
|
91
|
+
|
|
92
|
+
const fastify = require('fastify')({
|
|
93
|
+
logger: {
|
|
94
|
+
level: 'info',
|
|
95
|
+
stream: stream
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Advanced Logger Configuration
|
|
101
|
+
|
|
102
|
+
<a id="logging-request-id"></a>
|
|
103
|
+
#### Request ID Tracking
|
|
104
|
+
By default, Fastify adds an ID to every request for easier tracking. If the
|
|
105
|
+
`requestIdHeader` option is set and the corresponding header is present, its
|
|
106
|
+
value is used; otherwise, a new incremental ID is generated. See Fastify Factory
|
|
107
|
+
[`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify Factory
|
|
108
|
+
[`genReqId`](./Server.md#genreqid) for customization options.
|
|
109
|
+
|
|
110
|
+
> ⚠ Warning:
|
|
111
|
+
> Enabling `requestIdHeader` allows any callers to set `reqId` to a
|
|
112
|
+
> value of their choosing.
|
|
113
|
+
> No validation is performed on `requestIdHeader`.
|
|
114
|
+
|
|
115
|
+
#### Serializers
|
|
116
|
+
The default logger uses standard serializers for objects with `req`, `res`, and
|
|
117
|
+
`err` properties. The `req` object is the Fastify [`Request`](./Request.md)
|
|
118
|
+
object, and the `res` object is the Fastify [`Reply`](./Reply.md) object. This
|
|
119
|
+
behavior can be customized with custom serializers.
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
const fastify = require('fastify')({
|
|
123
|
+
logger: {
|
|
124
|
+
serializers: {
|
|
125
|
+
req (request) {
|
|
126
|
+
return { url: request.url }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
For example, the response payload and headers could be logged using the approach
|
|
133
|
+
below (not recommended):
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
const fastify = require('fastify')({
|
|
137
|
+
logger: {
|
|
138
|
+
transport: {
|
|
139
|
+
target: 'pino-pretty'
|
|
140
|
+
},
|
|
141
|
+
serializers: {
|
|
142
|
+
res (reply) {
|
|
143
|
+
// The default
|
|
144
|
+
return {
|
|
145
|
+
statusCode: reply.statusCode
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
req (request) {
|
|
149
|
+
return {
|
|
150
|
+
method: request.method,
|
|
151
|
+
url: request.url,
|
|
152
|
+
path: request.routeOptions.url,
|
|
153
|
+
parameters: request.params,
|
|
154
|
+
// Including headers in the log could violate privacy laws,
|
|
155
|
+
// e.g., GDPR. Use the "redact" option to remove sensitive
|
|
156
|
+
// fields. It could also leak authentication data in the logs.
|
|
157
|
+
headers: request.headers
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> ℹ️ Note:
|
|
166
|
+
> In some cases, the [`Reply`](./Reply.md) object passed to the `res`
|
|
167
|
+
> serializer cannot be fully constructed. When writing a custom `res`
|
|
168
|
+
> serializer, check for the existence of any properties on `reply` aside from
|
|
169
|
+
> `statusCode`, which is always present. For example, verify the existence of
|
|
170
|
+
> `getHeaders` before calling it:
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
const fastify = require('fastify')({
|
|
174
|
+
logger: {
|
|
175
|
+
transport: {
|
|
176
|
+
target: 'pino-pretty'
|
|
177
|
+
},
|
|
178
|
+
serializers: {
|
|
179
|
+
res (reply) {
|
|
180
|
+
// The default
|
|
181
|
+
return {
|
|
182
|
+
statusCode: reply.statusCode,
|
|
183
|
+
headers: typeof reply.getHeaders === 'function'
|
|
184
|
+
? reply.getHeaders()
|
|
185
|
+
: {}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
> ℹ️ Note:
|
|
194
|
+
> The body cannot be serialized inside a `req` method because the
|
|
195
|
+
> request is serialized when the child logger is created. At that time, the body
|
|
196
|
+
> is not yet parsed.
|
|
197
|
+
|
|
198
|
+
See the following approach to log `req.body`:
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
app.addHook('preHandler', function (req, reply, done) {
|
|
202
|
+
if (req.body) {
|
|
203
|
+
req.log.info({ body: req.body }, 'parsed body')
|
|
204
|
+
}
|
|
205
|
+
done()
|
|
206
|
+
})
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
> ℹ️ Note:
|
|
210
|
+
> Ensure serializers never throw errors, as this can cause the Node
|
|
211
|
+
> process to exit. See the
|
|
212
|
+
> [Pino documentation](https://getpino.io/#/docs/api?id=opt-serializers) for more
|
|
213
|
+
> information.
|
|
214
|
+
|
|
215
|
+
*Any logger other than Pino will ignore this option.*
|
|
216
|
+
|
|
217
|
+
### Using Custom Loggers
|
|
218
|
+
A custom logger instance can be supplied by passing it as `loggerInstance`. The
|
|
219
|
+
logger must conform to the Pino interface, with methods: `info`, `error`,
|
|
220
|
+
`debug`, `fatal`, `warn`, `trace`, `silent`, `child`, and a string property
|
|
221
|
+
`level`.
|
|
222
|
+
|
|
223
|
+
Example:
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
const log = require('pino')({ level: 'info' })
|
|
227
|
+
const fastify = require('fastify')({ loggerInstance: log })
|
|
228
|
+
|
|
229
|
+
log.info('does not have request information')
|
|
230
|
+
|
|
231
|
+
fastify.get('/', function (request, reply) {
|
|
232
|
+
request.log.info('includes request information, but is the same logger instance as `log`')
|
|
233
|
+
reply.send({ hello: 'world' })
|
|
234
|
+
})
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
*The logger instance for the current request is available in every part of the
|
|
238
|
+
[lifecycle](./Lifecycle.md).*
|
|
239
|
+
|
|
240
|
+
### Log Redaction
|
|
241
|
+
|
|
242
|
+
[Pino](https://getpino.io) supports low-overhead log redaction for obscuring
|
|
243
|
+
values of specific properties in recorded logs. For example, log all HTTP
|
|
244
|
+
headers except the `Authorization` header for security:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
const fastify = Fastify({
|
|
248
|
+
logger: {
|
|
249
|
+
stream: stream,
|
|
250
|
+
redact: ['req.headers.authorization'],
|
|
251
|
+
level: 'info',
|
|
252
|
+
serializers: {
|
|
253
|
+
req (request) {
|
|
254
|
+
return {
|
|
255
|
+
method: request.method,
|
|
256
|
+
url: request.url,
|
|
257
|
+
headers: request.headers,
|
|
258
|
+
host: request.host,
|
|
259
|
+
remoteAddress: request.ip,
|
|
260
|
+
remotePort: request.socket.remotePort
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
See https://getpino.io/#/docs/redaction for more details.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<h1 align="center">Fastify</h1>
|
|
2
|
+
|
|
3
|
+
## Middleware
|
|
4
|
+
|
|
5
|
+
Starting with Fastify v3.0.0, middleware is not supported out of the box and
|
|
6
|
+
requires an external plugin such as
|
|
7
|
+
[`@fastify/express`](https://github.com/fastify/fastify-express) or
|
|
8
|
+
[`@fastify/middie`](https://github.com/fastify/middie).
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
An example of registering the
|
|
12
|
+
[`@fastify/express`](https://github.com/fastify/fastify-express) plugin to `use`
|
|
13
|
+
Express middleware:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
await fastify.register(require('@fastify/express'))
|
|
17
|
+
fastify.use(require('cors')())
|
|
18
|
+
fastify.use(require('dns-prefetch-control')())
|
|
19
|
+
fastify.use(require('frameguard')())
|
|
20
|
+
fastify.use(require('hsts')())
|
|
21
|
+
fastify.use(require('ienoopen')())
|
|
22
|
+
fastify.use(require('x-xss-protection')())
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
[`@fastify/middie`](https://github.com/fastify/middie) can also be used,
|
|
26
|
+
which provides support for simple Express-style middleware with improved
|
|
27
|
+
performance:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
await fastify.register(require('@fastify/middie'))
|
|
31
|
+
fastify.use(require('cors')())
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Middleware can be encapsulated, allowing control over where it runs using
|
|
35
|
+
`register` as explained in the [plugins guide](../Guides/Plugins-Guide.md).
|
|
36
|
+
|
|
37
|
+
Fastify middleware does not expose the `send` method or other methods specific
|
|
38
|
+
to the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps
|
|
39
|
+
the incoming `req` and `res` Node instances using the
|
|
40
|
+
[Request](./Request.md#request) and [Reply](./Reply.md#reply) objects
|
|
41
|
+
internally, but this is done after the middleware phase. To create middleware,
|
|
42
|
+
use the Node `req` and `res` instances. Alternatively, use the `preHandler` hook
|
|
43
|
+
that already has the Fastify [Request](./Request.md#request) and
|
|
44
|
+
[Reply](./Reply.md#reply) instances. For more information, see
|
|
45
|
+
[Hooks](./Hooks.md#hooks).
|
|
46
|
+
|
|
47
|
+
#### Restrict middleware execution to certain paths
|
|
48
|
+
<a id="restrict-usage"></a>
|
|
49
|
+
|
|
50
|
+
To run middleware under certain paths, pass the path as the first parameter to
|
|
51
|
+
`use`.
|
|
52
|
+
|
|
53
|
+
> ℹ️ Note:
|
|
54
|
+
> This does not support routes with parameters
|
|
55
|
+
> (e.g. `/user/:id/comments`) and wildcards are not supported in multiple paths.
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const path = require('node:path')
|
|
59
|
+
const serveStatic = require('serve-static')
|
|
60
|
+
|
|
61
|
+
// Single path
|
|
62
|
+
fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))
|
|
63
|
+
|
|
64
|
+
// Wildcard path
|
|
65
|
+
fastify.use('/css/(.*)', serveStatic(path.join(__dirname, '/assets')))
|
|
66
|
+
|
|
67
|
+
// Multiple paths
|
|
68
|
+
fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Alternatives
|
|
72
|
+
|
|
73
|
+
Fastify offers alternatives to commonly used middleware, such as
|
|
74
|
+
[`@fastify/helmet`](https://github.com/fastify/fastify-helmet) for
|
|
75
|
+
[`helmet`](https://github.com/helmetjs/helmet),
|
|
76
|
+
[`@fastify/cors`](https://github.com/fastify/fastify-cors) for
|
|
77
|
+
[`cors`](https://github.com/expressjs/cors), and
|
|
78
|
+
[`@fastify/static`](https://github.com/fastify/fastify-static) for
|
|
79
|
+
[`serve-static`](https://github.com/expressjs/serve-static).
|