fastify 3.24.0 → 3.25.2
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/README.md +30 -29
- package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
- package/docs/Guides/Ecosystem.md +513 -0
- package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
- package/docs/{Getting-Started.md → Guides/Getting-Started.md} +180 -60
- package/docs/Guides/Index.md +30 -4
- package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
- package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
- package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
- package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
- package/docs/Guides/Style-Guide.md +246 -0
- package/docs/{Testing.md → Guides/Testing.md} +26 -12
- package/docs/Guides/Write-Plugin.md +102 -0
- package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
- package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
- package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
- package/docs/{Errors.md → Reference/Errors.md} +77 -47
- package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
- package/docs/{Hooks.md → Reference/Hooks.md} +157 -70
- package/docs/Reference/Index.md +71 -0
- package/docs/{LTS.md → Reference/LTS.md} +31 -32
- package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
- package/docs/{Logging.md → Reference/Logging.md} +68 -28
- package/docs/Reference/Middleware.md +78 -0
- package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
- package/docs/{Reply.md → Reference/Reply.md} +205 -94
- package/docs/{Request.md → Reference/Request.md} +32 -16
- package/docs/{Routes.md → Reference/Routes.md} +243 -113
- package/docs/{Server.md → Reference/Server.md} +516 -267
- package/docs/{TypeScript.md → Reference/TypeScript.md} +451 -191
- package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
- package/docs/index.md +24 -0
- package/examples/typescript-server.ts +1 -1
- package/fastify.js +2 -3
- package/lib/contentTypeParser.js +11 -6
- package/lib/decorate.js +6 -3
- package/lib/logger.js +1 -1
- package/lib/route.js +1 -1
- package/lib/server.js +9 -8
- package/package.json +9 -4
- package/test/als.test.js +74 -0
- package/test/constrained-routes.test.js +220 -0
- package/test/custom-parser.test.js +11 -2
- package/test/decorator.test.js +38 -0
- package/test/handler-context.test.js +11 -4
- package/test/http2/closing.test.js +14 -5
- package/test/http2/constraint.test.js +91 -0
- package/test/listen.test.js +36 -22
- package/test/logger.test.js +16 -0
- package/test/maxRequestsPerSocket.test.js +10 -0
- package/test/request-error.test.js +2 -8
- package/test/requestTimeout.test.js +4 -1
- package/test/router-options.test.js +10 -1
- package/test/schema-feature.test.js +146 -0
- package/test/stream.test.js +14 -3
- package/test/trust-proxy.test.js +15 -7
- package/test/types/instance.test-d.ts +52 -1
- package/test/types/request.test-d.ts +7 -1
- package/test/types/route.test-d.ts +21 -0
- package/types/hooks.d.ts +12 -1
- package/types/instance.d.ts +16 -6
- package/types/request.d.ts +4 -1
- package/types/route.d.ts +1 -1
- package/docs/Ecosystem.md +0 -211
- package/docs/Middleware.md +0 -53
- package/docs/Style-Guide.md +0 -185
- package/docs/Write-Plugin.md +0 -58
|
@@ -2,35 +2,63 @@
|
|
|
2
2
|
|
|
3
3
|
## Logging
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
enable it at runtime. We use
|
|
9
|
-
[abstract-logging](https://www.npmjs.com/package/abstract-logging) for
|
|
10
|
-
|
|
5
|
+
### Enable logging
|
|
6
|
+
Logging is disabled by default, and you can enable it by passing `{ logger: true
|
|
7
|
+
}` or `{ logger: { level: 'info' } }` when you create a Fastify instance. Note
|
|
8
|
+
that if the logger is disabled, it is impossible to enable it at runtime. We use
|
|
9
|
+
[abstract-logging](https://www.npmjs.com/package/abstract-logging) for this
|
|
10
|
+
purpose.
|
|
11
11
|
|
|
12
|
-
As Fastify is focused on performance, it uses
|
|
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, when enabled, set to `'info'`.
|
|
13
15
|
|
|
14
|
-
Enabling the
|
|
16
|
+
Enabling the production JSON logger:
|
|
15
17
|
|
|
16
18
|
```js
|
|
17
19
|
const fastify = require('fastify')({
|
|
18
20
|
logger: true
|
|
19
21
|
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Enabling the logger with appropriate configuration for both local development
|
|
25
|
+
and production environment requires bit more configuration:
|
|
26
|
+
```js
|
|
27
|
+
const fastify = require('fastify')({
|
|
28
|
+
logger: {
|
|
29
|
+
prettyPrint:
|
|
30
|
+
environment === 'development'
|
|
31
|
+
? {
|
|
32
|
+
translateTime: 'HH:MM:ss Z',
|
|
33
|
+
ignore: 'pid,hostname'
|
|
34
|
+
}
|
|
35
|
+
: false
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
⚠️ `pino-pretty` needs to be installed as a dev dependency, it is not included
|
|
40
|
+
by default for performance reasons.
|
|
20
41
|
|
|
42
|
+
### Usage
|
|
43
|
+
You can use the logger like this in your route handlers:
|
|
44
|
+
|
|
45
|
+
```js
|
|
21
46
|
fastify.get('/', options, function (request, reply) {
|
|
22
47
|
request.log.info('Some info about the current request')
|
|
23
48
|
reply.send({ hello: 'world' })
|
|
24
49
|
})
|
|
25
50
|
```
|
|
26
51
|
|
|
27
|
-
You can trigger new logs outside route handlers by using the Pino instance from
|
|
52
|
+
You can trigger new logs outside route handlers by using the Pino instance from
|
|
53
|
+
the Fastify instance:
|
|
28
54
|
```js
|
|
29
55
|
fastify.log.info('Something important happened!');
|
|
30
56
|
```
|
|
31
57
|
|
|
32
|
-
If you want to pass some options to the logger, just pass them to Fastify.
|
|
33
|
-
|
|
58
|
+
If you want to pass some options to the logger, just pass them to Fastify. You
|
|
59
|
+
can find all available options in the [Pino
|
|
60
|
+
documentation](https://github.com/pinojs/pino/blob/master/docs/api.md#pinooptions-stream).
|
|
61
|
+
If you want to specify a file destination, use:
|
|
34
62
|
|
|
35
63
|
```js
|
|
36
64
|
const fastify = require('fastify')({
|
|
@@ -46,7 +74,8 @@ fastify.get('/', options, function (request, reply) {
|
|
|
46
74
|
})
|
|
47
75
|
```
|
|
48
76
|
|
|
49
|
-
If you want to pass a custom stream to the Pino instance, just add a stream
|
|
77
|
+
If you want to pass a custom stream to the Pino instance, just add a stream
|
|
78
|
+
field to the logger object.
|
|
50
79
|
|
|
51
80
|
```js
|
|
52
81
|
const split = require('split2')
|
|
@@ -60,12 +89,19 @@ const fastify = require('fastify')({
|
|
|
60
89
|
})
|
|
61
90
|
```
|
|
62
91
|
|
|
63
|
-
<a
|
|
92
|
+
<a id="logging-request-id"></a>
|
|
64
93
|
|
|
65
|
-
By default, Fastify adds an ID to every request for easier tracking. If the
|
|
94
|
+
By default, Fastify adds an ID to every request for easier tracking. If the
|
|
95
|
+
"request-id" header is present its value is used, otherwise a new incremental ID
|
|
96
|
+
is generated. See Fastify Factory
|
|
97
|
+
[`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify Factory
|
|
98
|
+
[`genReqId`](./Server.md#genreqid) for customization options.
|
|
66
99
|
|
|
67
|
-
The default logger is configured with a set of standard serializers that
|
|
68
|
-
|
|
100
|
+
The default logger is configured with a set of standard serializers that
|
|
101
|
+
serialize objects with `req`, `res`, and `err` properties. The object received
|
|
102
|
+
by `req` is the Fastify [`Request`](./Request.md) object, while the object
|
|
103
|
+
received by `res` is the Fastify [`Reply`](./Reply.md) object. This behaviour
|
|
104
|
+
can be customized by specifying custom serializers.
|
|
69
105
|
```js
|
|
70
106
|
const fastify = require('fastify')({
|
|
71
107
|
logger: {
|
|
@@ -77,7 +113,8 @@ const fastify = require('fastify')({
|
|
|
77
113
|
}
|
|
78
114
|
})
|
|
79
115
|
```
|
|
80
|
-
For example, the response payload and headers could be logged using the approach
|
|
116
|
+
For example, the response payload and headers could be logged using the approach
|
|
117
|
+
below (even if it is *not recommended*):
|
|
81
118
|
|
|
82
119
|
```js
|
|
83
120
|
const fastify = require('fastify')({
|
|
@@ -94,8 +131,8 @@ const fastify = require('fastify')({
|
|
|
94
131
|
return {
|
|
95
132
|
method: request.method,
|
|
96
133
|
url: request.url,
|
|
97
|
-
path: request.
|
|
98
|
-
parameters: request.
|
|
134
|
+
path: request.routerPath,
|
|
135
|
+
parameters: request.params,
|
|
99
136
|
// Including the headers in the log could be in violation
|
|
100
137
|
// of privacy laws, e.g. GDPR. You should use the "redact" option to
|
|
101
138
|
// remove sensitive fields. It could also leak authentication data in
|
|
@@ -107,7 +144,9 @@ const fastify = require('fastify')({
|
|
|
107
144
|
}
|
|
108
145
|
});
|
|
109
146
|
```
|
|
110
|
-
**Note**: The body cannot be serialized inside a `req` method because the
|
|
147
|
+
**Note**: The body cannot be serialized inside a `req` method because the
|
|
148
|
+
request is serialized when we create the child logger. At that time, the body is
|
|
149
|
+
not yet parsed.
|
|
111
150
|
|
|
112
151
|
See an approach to log `req.body`
|
|
113
152
|
|
|
@@ -123,9 +162,10 @@ app.addHook('preHandler', function (req, reply, done) {
|
|
|
123
162
|
|
|
124
163
|
*Any logger other than Pino will ignore this option.*
|
|
125
164
|
|
|
126
|
-
You can also supply your own logger instance. Instead of passing configuration
|
|
127
|
-
The logger you supply must conform to the Pino
|
|
128
|
-
|
|
165
|
+
You can also supply your own logger instance. Instead of passing configuration
|
|
166
|
+
options, pass the instance. The logger you supply must conform to the Pino
|
|
167
|
+
interface; that is, it must have the following methods: `info`, `error`,
|
|
168
|
+
`debug`, `fatal`, `warn`, `trace`, `child`.
|
|
129
169
|
|
|
130
170
|
Example:
|
|
131
171
|
|
|
@@ -141,14 +181,14 @@ fastify.get('/', function (request, reply) {
|
|
|
141
181
|
})
|
|
142
182
|
```
|
|
143
183
|
|
|
144
|
-
*The logger instance for the current request is available in every part of the
|
|
184
|
+
*The logger instance for the current request is available in every part of the
|
|
185
|
+
[lifecycle](./Lifecycle.md).*
|
|
145
186
|
|
|
146
187
|
## Log Redaction
|
|
147
188
|
|
|
148
|
-
[Pino](https://getpino.io) supports low-overhead log redaction for
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
`Authorization` header for security concerns:
|
|
189
|
+
[Pino](https://getpino.io) supports low-overhead log redaction for obscuring
|
|
190
|
+
values of specific properties in recorded logs. As an example, we might want to
|
|
191
|
+
log all the HTTP headers minus the `Authorization` header for security concerns:
|
|
152
192
|
|
|
153
193
|
```js
|
|
154
194
|
const fastify = Fastify({
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
[`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
|
+
You can also use [`middie`](https://github.com/fastify/middie), which provides
|
|
26
|
+
support for simple Express-style middleware but with improved performance:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
await fastify.register(require('middie'))
|
|
30
|
+
fastify.use(require('cors')())
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Remember that middleware can be encapsulated; this means that you can decide
|
|
34
|
+
where your middleware should run by using `register` as explained in the
|
|
35
|
+
[plugins guide](../Guides/Plugins-Guide.md).
|
|
36
|
+
|
|
37
|
+
Fastify middleware do not expose the `send` method or other methods specific to
|
|
38
|
+
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. If you need to create
|
|
42
|
+
middleware, you have to use the Node `req` and `res` instances. Otherwise, you
|
|
43
|
+
can use the `preHandler` hook that already has the
|
|
44
|
+
[Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances.
|
|
45
|
+
For more information, see [Hooks](./Hooks.md#hooks).
|
|
46
|
+
|
|
47
|
+
#### Restrict middleware execution to certain paths
|
|
48
|
+
<a id="restrict-usage"></a>
|
|
49
|
+
|
|
50
|
+
If you need to only run middleware under certain paths, just pass the path as
|
|
51
|
+
the first parameter to `use` and you are done!
|
|
52
|
+
|
|
53
|
+
*Note that this does not support routes with parameters, (e.g.
|
|
54
|
+
`/user/:id/comments`) and wildcards are not supported in multiple paths.*
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
const path = require('path')
|
|
58
|
+
const serveStatic = require('serve-static')
|
|
59
|
+
|
|
60
|
+
// Single path
|
|
61
|
+
fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))
|
|
62
|
+
|
|
63
|
+
// Wildcard path
|
|
64
|
+
fastify.use('/css/(.*)', serveStatic(path.join(__dirname, '/assets')))
|
|
65
|
+
|
|
66
|
+
// Multiple paths
|
|
67
|
+
fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Alternatives
|
|
71
|
+
|
|
72
|
+
Fastify offers some alternatives to the most commonly used middleware, such as
|
|
73
|
+
[`fastify-helmet`](https://github.com/fastify/fastify-helmet) in case of
|
|
74
|
+
[`helmet`](https://github.com/helmetjs/helmet),
|
|
75
|
+
[`fastify-cors`](https://github.com/fastify/fastify-cors) for
|
|
76
|
+
[`cors`](https://github.com/expressjs/cors), and
|
|
77
|
+
[`fastify-static`](https://github.com/fastify/fastify-static) for
|
|
78
|
+
[`serve-static`](https://github.com/expressjs/serve-static).
|
|
@@ -1,27 +1,42 @@
|
|
|
1
1
|
<h1 align="center">Fastify</h1>
|
|
2
2
|
|
|
3
3
|
## Plugins
|
|
4
|
-
Fastify allows the user to extend its functionalities with plugins.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
Fastify allows the user to extend its functionalities with plugins. A plugin can
|
|
5
|
+
be a set of routes, a server [decorator](./Decorators.md), or whatever. The API
|
|
6
|
+
that you will need to use one or more plugins, is `register`.
|
|
7
|
+
|
|
8
|
+
By default, `register` creates a *new scope*, this means that if you make some
|
|
9
|
+
changes to the Fastify instance (via `decorate`), this change will not be
|
|
10
|
+
reflected by the current context ancestors, but only to its descendants. This
|
|
11
|
+
feature allows us to achieve plugin *encapsulation* and *inheritance*, in this
|
|
12
|
+
way we create a *direct acyclic graph* (DAG) and we will not have issues caused
|
|
13
|
+
by cross dependencies.
|
|
14
|
+
|
|
15
|
+
You already see in the [getting started](../Guides/Getting-Started.md#your-first-plugin)
|
|
16
|
+
section how using this API is pretty straightforward.
|
|
10
17
|
```
|
|
11
18
|
fastify.register(plugin, [options])
|
|
12
19
|
```
|
|
13
20
|
|
|
14
|
-
<a name="plugin-options"></a>
|
|
15
21
|
### Plugin Options
|
|
16
|
-
|
|
22
|
+
<a id="plugin-options"></a>
|
|
23
|
+
|
|
24
|
+
The optional `options` parameter for `fastify.register` supports a predefined
|
|
25
|
+
set of options that Fastify itself will use, except when the plugin has been
|
|
26
|
+
wrapped with [fastify-plugin](https://github.com/fastify/fastify-plugin). This
|
|
27
|
+
options object will also be passed to the plugin upon invocation, regardless of
|
|
28
|
+
whether or not the plugin has been wrapped. The currently supported list of
|
|
29
|
+
Fastify specific options is:
|
|
17
30
|
|
|
18
|
-
+ [`logLevel`](Routes.md#custom-log-level)
|
|
19
|
-
+ [`logSerializers`](Routes.md#custom-log-serializer)
|
|
20
|
-
+ [`prefix`](
|
|
31
|
+
+ [`logLevel`](./Routes.md#custom-log-level)
|
|
32
|
+
+ [`logSerializers`](./Routes.md#custom-log-serializer)
|
|
33
|
+
+ [`prefix`](#route-prefixing-option)
|
|
21
34
|
|
|
22
35
|
**Note: Those options will be ignored when used with fastify-plugin**
|
|
23
36
|
|
|
24
|
-
It is possible that Fastify will directly support other options in the future.
|
|
37
|
+
It is possible that Fastify will directly support other options in the future.
|
|
38
|
+
Thus, to avoid collisions, a plugin should consider namespacing its options. For
|
|
39
|
+
example, a plugin `foo` might be registered like so:
|
|
25
40
|
|
|
26
41
|
```js
|
|
27
42
|
fastify.register(require('fastify-foo'), {
|
|
@@ -33,7 +48,8 @@ fastify.register(require('fastify-foo'), {
|
|
|
33
48
|
})
|
|
34
49
|
```
|
|
35
50
|
|
|
36
|
-
If collisions are not a concern, the plugin may simply accept the options object
|
|
51
|
+
If collisions are not a concern, the plugin may simply accept the options object
|
|
52
|
+
as-is:
|
|
37
53
|
|
|
38
54
|
```js
|
|
39
55
|
fastify.register(require('fastify-foo'), {
|
|
@@ -43,7 +59,9 @@ fastify.register(require('fastify-foo'), {
|
|
|
43
59
|
})
|
|
44
60
|
```
|
|
45
61
|
|
|
46
|
-
The `options` parameter can also be a `Function` that will be evaluated at the
|
|
62
|
+
The `options` parameter can also be a `Function` that will be evaluated at the
|
|
63
|
+
time the plugin is registered while giving access to the Fastify instance via
|
|
64
|
+
the first positional argument:
|
|
47
65
|
|
|
48
66
|
```js
|
|
49
67
|
const fp = require('fastify-plugin')
|
|
@@ -58,19 +76,40 @@ fastify.register(fp((fastify, opts, done) => {
|
|
|
58
76
|
fastify.register(require('fastify-foo'), parent => parent.foo_bar)
|
|
59
77
|
```
|
|
60
78
|
|
|
61
|
-
The Fastify instance passed on to the function is the latest state of the
|
|
79
|
+
The Fastify instance passed on to the function is the latest state of the
|
|
80
|
+
**external Fastify instance** the plugin was declared on, allowing access to
|
|
81
|
+
variables injected via [`decorate`](./Decorators.md) by preceding plugins
|
|
82
|
+
according to the **order of registration**. This is useful in case a plugin
|
|
83
|
+
depends on changes made to the Fastify instance by a preceding plugin i.e.
|
|
84
|
+
utilizing an existing database connection to wrap around it.
|
|
62
85
|
|
|
63
|
-
Keep in mind that the Fastify instance passed on to the function is the same as
|
|
86
|
+
Keep in mind that the Fastify instance passed on to the function is the same as
|
|
87
|
+
the one that will be passed into the plugin, a copy of the external Fastify
|
|
88
|
+
instance rather than a reference. Any usage of the instance will behave the same
|
|
89
|
+
as it would if called within the plugins function i.e. if `decorate` is called,
|
|
90
|
+
the decorated variables will be available within the plugins function unless it
|
|
91
|
+
was wrapped with [`fastify-plugin`](https://github.com/fastify/fastify-plugin).
|
|
64
92
|
|
|
65
|
-
<a name="route-prefixing-option"></a>
|
|
66
93
|
#### Route Prefixing option
|
|
67
|
-
|
|
68
|
-
|
|
94
|
+
<a id="route-prefixing-option"></a>
|
|
95
|
+
|
|
96
|
+
If you pass an option with the key `prefix` with a `string` value, Fastify will
|
|
97
|
+
use it to prefix all the routes inside the register, for more info check
|
|
98
|
+
[here](./Routes.md#route-prefixing).
|
|
99
|
+
|
|
100
|
+
Be aware that if you use
|
|
101
|
+
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) this option will
|
|
102
|
+
not work.
|
|
69
103
|
|
|
70
|
-
<a name="error-handling"></a>
|
|
71
104
|
#### Error handling
|
|
72
|
-
|
|
73
|
-
|
|
105
|
+
<a id="error-handling"></a>
|
|
106
|
+
|
|
107
|
+
The error handling is done by
|
|
108
|
+
[avvio](https://github.com/mcollina/avvio#error-handling).
|
|
109
|
+
|
|
110
|
+
As a general rule, it is highly recommended that you handle your errors in the
|
|
111
|
+
next `after` or `ready` block, otherwise you will get them inside the `listen`
|
|
112
|
+
callback.
|
|
74
113
|
|
|
75
114
|
```js
|
|
76
115
|
fastify.register(require('my-plugin'))
|
|
@@ -90,8 +129,8 @@ fastify.listen(3000, (err, address) => {
|
|
|
90
129
|
})
|
|
91
130
|
```
|
|
92
131
|
|
|
93
|
-
<a name="async-await"></a>
|
|
94
132
|
### async/await
|
|
133
|
+
<a id="async-await"></a>
|
|
95
134
|
|
|
96
135
|
*async/await* is supported by `after`, `ready` and `listen`, as well as
|
|
97
136
|
`fastify` being a [Thenable](https://promisesaplus.com/).
|
|
@@ -106,10 +145,11 @@ await fastify.ready()
|
|
|
106
145
|
await fastify.listen(3000)
|
|
107
146
|
```
|
|
108
147
|
|
|
109
|
-
<a name="esm-support"></a>
|
|
110
148
|
#### ESM support
|
|
149
|
+
<a id="esm-support"></a>
|
|
111
150
|
|
|
112
|
-
ESM is supported as well from [Node.js
|
|
151
|
+
ESM is supported as well from [Node.js
|
|
152
|
+
`v13.3.0`](https://nodejs.org/api/esm.html) and above!
|
|
113
153
|
|
|
114
154
|
```js
|
|
115
155
|
// main.mjs
|
|
@@ -131,9 +171,13 @@ async function plugin (fastify, opts) {
|
|
|
131
171
|
export default plugin
|
|
132
172
|
```
|
|
133
173
|
|
|
134
|
-
<a name="create-plugin"></a>
|
|
135
174
|
### Create a plugin
|
|
136
|
-
|
|
175
|
+
<a id="create-plugin"></a>
|
|
176
|
+
|
|
177
|
+
Creating a plugin is very easy, you just need to create a function that takes
|
|
178
|
+
three parameters, the `fastify` instance, an `options` object, and the `done`
|
|
179
|
+
callback.
|
|
180
|
+
|
|
137
181
|
Example:
|
|
138
182
|
```js
|
|
139
183
|
module.exports = function (fastify, opts, done) {
|
|
@@ -156,19 +200,28 @@ module.exports = function (fastify, opts, done) {
|
|
|
156
200
|
done()
|
|
157
201
|
}
|
|
158
202
|
```
|
|
159
|
-
Sometimes, you will need to know when the server is about to close, for example,
|
|
203
|
+
Sometimes, you will need to know when the server is about to close, for example,
|
|
204
|
+
because you must close a connection to a database. To know when this is going to
|
|
205
|
+
happen, you can use the [`'onClose'`](./Hooks.md#on-close) hook.
|
|
160
206
|
|
|
161
|
-
Do not forget that `register` will always create a new Fastify scope, if you do
|
|
207
|
+
Do not forget that `register` will always create a new Fastify scope, if you do
|
|
208
|
+
not need that, read the following section.
|
|
162
209
|
|
|
163
|
-
<a name="handle-scope"></a>
|
|
164
210
|
### Handle the scope
|
|
165
|
-
|
|
211
|
+
<a id="handle-scope"></a>
|
|
212
|
+
|
|
213
|
+
If you are using `register` only for extending the functionality of the server
|
|
214
|
+
with [`decorate`](./Decorators.md), it is your responsibility to tell Fastify
|
|
215
|
+
not to create a new scope. Otherwise, your changes will not be accessible by the
|
|
216
|
+
user in the upper scope.
|
|
166
217
|
|
|
167
218
|
You have two ways to tell Fastify to avoid the creation of a new context:
|
|
168
219
|
- Use the [`fastify-plugin`](https://github.com/fastify/fastify-plugin) module
|
|
169
220
|
- Use the `'skip-override'` hidden property
|
|
170
221
|
|
|
171
|
-
We recommend using the `fastify-plugin` module, because it solves this problem
|
|
222
|
+
We recommend using the `fastify-plugin` module, because it solves this problem
|
|
223
|
+
for you, and you can pass a version range of Fastify as a parameter that your
|
|
224
|
+
plugin will support.
|
|
172
225
|
```js
|
|
173
226
|
const fp = require('fastify-plugin')
|
|
174
227
|
|
|
@@ -177,9 +230,13 @@ module.exports = fp(function (fastify, opts, done) {
|
|
|
177
230
|
done()
|
|
178
231
|
}, '0.x')
|
|
179
232
|
```
|
|
180
|
-
Check the [`fastify-plugin`](https://github.com/fastify/fastify-plugin)
|
|
233
|
+
Check the [`fastify-plugin`](https://github.com/fastify/fastify-plugin)
|
|
234
|
+
documentation to learn more about how to use this module.
|
|
181
235
|
|
|
182
|
-
If you do not use the `fastify-plugin` module, you can use the `'skip-override'`
|
|
236
|
+
If you do not use the `fastify-plugin` module, you can use the `'skip-override'`
|
|
237
|
+
hidden property, but we do not recommend it. If in the future the Fastify API
|
|
238
|
+
changes it will be your responsibility to update the module, while if you use
|
|
239
|
+
`fastify-plugin`, you can be sure about backward compatibility.
|
|
183
240
|
```js
|
|
184
241
|
function yourPlugin (fastify, opts, done) {
|
|
185
242
|
fastify.decorate('utility', () => {})
|