You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/ContentTypeParser.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ Natively, Fastify only supports `'application/json'` and `'text/plain'` content
5
5
6
6
As with the other APIs, `addContentTypeParser` is encapsulated in the scope in which it is declared. This means that if you declare it in the root scope it will be available everywhere, while if you declare it inside a plugin it will be available only in that scope and its children.
7
7
8
-
Fastify automatically adds the parsed request payload to the [Fastify request](./Request.md) object which you can access with `request.body`.
8
+
Fastify automatically adds the parsed request payload to the [Fastify request](Request.md) object which you can access with `request.body`.
9
9
10
10
### Usage
11
11
```js
@@ -45,7 +45,7 @@ if (!fastify.hasContentTypeParser('application/jsoff')){
45
45
**Notice**: The old syntaxes `function(req, done)` and `async function(req)` for the parser are still supported but they are deprecated.
46
46
47
47
#### Body Parser
48
-
You can parse the body of a request in two ways. The first one is shown above: you add a custom content type parser and handle the request stream. In the second one, you should pass a `parseAs` option to the `addContentTypeParser` API, where you declare how you want to get the body. It could be of type `'string'` or `'buffer'`. If you use the `parseAs` option, Fastify will internally handle the stream and perform some checks, such as the [maximum size](./Server.md#factory-body-limit) of the body and the content length. If the limit is exceeded the custom parser will not be invoked.
48
+
You can parse the body of a request in two ways. The first one is shown above: you add a custom content type parser and handle the request stream. In the second one, you should pass a `parseAs` option to the `addContentTypeParser` API, where you declare how you want to get the body. It could be of type `'string'` or `'buffer'`. If you use the `parseAs` option, Fastify will internally handle the stream and perform some checks, such as the [maximum size](Server.md#factory-body-limit) of the body and the content length. If the limit is exceeded the custom parser will not be invoked.
49
49
```js
50
50
fastify.addContentTypeParser('application/json', { parseAs:'string' }, function (req, body, done) {
51
51
try {
@@ -62,7 +62,7 @@ See [`example/parser.js`](../examples/parser.js) for an example.
62
62
63
63
##### Custom Parser Options
64
64
+`parseAs` (string): Either `'string'` or `'buffer'` to designate how the incoming data should be collected. Default: `'buffer'`.
65
-
+`bodyLimit` (number): The maximum payload size, in bytes, that the custom parser will accept. Defaults to the global body limit passed to the [`Fastify factory function`](./Server.md#bodylimit).
65
+
+`bodyLimit` (number): The maximum payload size, in bytes, that the custom parser will accept. Defaults to the global body limit passed to the [`Fastify factory function`](Server.md#bodylimit).
66
66
67
67
#### Catch-All
68
68
There are some cases where you need to catch all requests regardless of their content type. With Fastify, you can just use the `'*'` content type.
@@ -113,4 +113,4 @@ fastify.route({
113
113
})
114
114
```
115
115
116
-
For piping file uploads you may want to checkout [this plugin](https://github.com/fastify/fastify-multipart).
116
+
For piping file uploads you may want to checkout [this plugin](https://github.com/fastify/fastify-multipart).
Copy file name to clipboardexpand all lines: docs/Errors.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,14 @@
8
8
9
9
Uncaught errors are likely to cause memory leaks, file descriptor leaks and other major production issues. [Domains](https://nodejs.org/en/docs/guides/domain-postmortem/) were introduced to try fixing this issue, but they did not. Given the fact that it is not possible to process all uncaught errors sensibly, the best way to deal with them at the moment is to [crash](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly). In case of promises, make sure to [handle](https://nodejs.org/dist/latest-v8.x/docs/api/deprecations.html#deprecations_dep0018_unhandled_promise_rejections) errors [correctly](https://github.com/mcollina/make-promises-safe).
10
10
11
-
Fastify follows an all-or-nothing approach and aims to be lean and optimal as much as possible. Thus, the developer is responsible for making sure that the errors are handled properly. Most of the errors are usually a result of unexpected input data, so we recommend specifying a [JSON.schema validation](./Validation-and-Serialization.md) for your input data.
11
+
Fastify follows an all-or-nothing approach and aims to be lean and optimal as much as possible. Thus, the developer is responsible for making sure that the errors are handled properly. Most of the errors are usually a result of unexpected input data, so we recommend specifying a [JSON.schema validation](Validation-and-Serialization.md) for your input data.
12
12
13
13
Fastify tries to catch as many uncaught errors it can without hindering performance. This includes:
14
14
15
15
1. synchronous routes, e.g. `app.get('/', () => { throw new Error('kaboom') })`
16
16
2.`async` routes, e.g. `app.get('/', async () => { throw new Error('kaboom') })`
17
17
18
-
In those two cases, the error will safely be caught by the promise and routed to the default error handler of Fastify for a generic `Internal Server Error` response. For customizing this behaviour, you should use [`setErrorHandler`](./Server.md#seterrorhandler).
18
+
In those two cases, the error will safely be caught by the promise and routed to the default error handler of Fastify for a generic `Internal Server Error` response. For customizing this behaviour, you should use [`setErrorHandler`](Server.md#seterrorhandler).
Copy file name to clipboardexpand all lines: docs/Fluent-Schema.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Fluent Schema
4
4
5
-
The [Validation and Serialization](./Validation-and-Serialization.md) documentation outlines all parameters accepted by Fastify to set up JSON Schema Validation in order to validate the input, and JSON Schema Serialization in order to optimize the output.
5
+
The [Validation and Serialization](Validation-and-Serialization.md) documentation outlines all parameters accepted by Fastify to set up JSON Schema Validation in order to validate the input, and JSON Schema Serialization in order to optimize the output.
6
6
7
7
[`fluent-schema`](https://github.com/fastify/fluent-schema) can be used to simplify this task while allowing the reuse of constants.
Copy file name to clipboardexpand all lines: docs/Getting-Started.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -85,7 +85,7 @@ Fastify offers an easy platform that helps to solve all of the problems outlined
85
85
### Your first plugin
86
86
As with JavaScript, where everything is an object, with Fastify everything is a plugin.<br>
87
87
Before digging into it, let's see how it works!<br>
88
-
Let's declare our basic server, but instead of declaring the route inside the entry point, we'll declare it in an external file (check out the [route declaration](./Routes.md) docs).
88
+
Let's declare our basic server, but instead of declaring the route inside the entry point, we'll declare it in an external file (check out the [route declaration](Routes.md) docs).
89
89
```js
90
90
constfastify=require('fastify')({
91
91
logger:true
@@ -195,12 +195,12 @@ module.exports = routes
195
195
Wow, that was fast!<br>
196
196
Let's recap what we have done here since we've introduced some new concepts.<br>
197
197
As you can see, we used `register` both for the database connector and the registration of the routes.
198
-
This is one of the best features of Fastify, it will load your plugins in the same order you declare them, and it will load the next plugin only once the current one has been loaded. In this way, we can register the database connector in the first plugin and use it in the second *(read [here](./Plugins.md#handle-the-scope) to understand how to handle the scope of a plugin)*.
198
+
This is one of the best features of Fastify, it will load your plugins in the same order you declare them, and it will load the next plugin only once the current one has been loaded. In this way, we can register the database connector in the first plugin and use it in the second *(read [here](Plugins.md#handle-the-scope) to understand how to handle the scope of a plugin)*.
199
199
Plugin loading starts when you call `fastify.listen()`, `fastify.inject()` or `fastify.ready()`
200
200
201
201
We have also used the `decorate` API to add custom objects to the Fastify namespace, making them available for use everywhere. Use of this API is encouraged to facilitate easy code reuse and to decrease code or logic duplication.
202
202
203
-
To dig deeper into how Fastify plugins work, how to develop new plugins, and for details on how to use the whole Fastify API to deal with the complexity of asynchronously bootstrapping an application, read [the hitchhiker's guide to plugins](./Plugins-Guide.md).
203
+
To dig deeper into how Fastify plugins work, how to develop new plugins, and for details on how to use the whole Fastify API to deal with the complexity of asynchronously bootstrapping an application, read [the hitchhiker's guide to plugins](Plugins-Guide.md).
This example shows how to pass an options object to the route, which accepts a `schema` key, that contains all of the schemas for route, `body`, `querystring`, `params` and `headers`.<br>
262
-
Read [Validation and Serialization](./Validation-and-Serialization.md) to learn more.
262
+
Read [Validation and Serialization](Validation-and-Serialization.md) to learn more.
Simply by specifying a schema as shown, you can speed up serialization by a factor of 2-3. This also helps to protect against leakage of potentially sensitive data, since Fastify will serialize only the data present in the response schema.
287
-
Read [Validation and Serialization](./Validation-and-Serialization.md) to learn more.
287
+
Read [Validation and Serialization](Validation-and-Serialization.md) to learn more.
288
288
289
289
<aname="extend-server"></a>
290
290
### Extend your server
291
291
Fastify is built to be extremely extensible and minimal, we believe that a bare bones framework is all that is necessary to make great applications possible.<br>
292
-
In other words, Fastify is not a "batteries included" framework, and relies on an amazing [ecosystem](./Ecosystem.md)!
292
+
In other words, Fastify is not a "batteries included" framework, and relies on an amazing [ecosystem](Ecosystem.md)!
293
293
294
294
<aname="test-server"></a>
295
295
### Test your server
296
296
Fastify does not offer a testing framework, but we do recommend a way to write your tests that uses the features and architecture of Fastify.<br>
297
-
Read the [testing](./Testing.md) documentation to learn more!
297
+
Read the [testing](Testing.md) documentation to learn more!
*The error will be handled by [`Reply`](./Reply.md#errors).*
222
+
*The error will be handled by [`Reply`](Reply.md#errors).*
223
223
224
224
Or if you're using `async/await` you can just throw an error:
225
225
```js
@@ -322,7 +322,7 @@ fastify.addHook('onReady', async function () {
322
322
323
323
<aname="on-close"></a>
324
324
### onClose
325
-
Triggered when `fastify.close()` is invoked to stop the server. It is useful when [plugins](./Plugins.md) need a "shutdown" event, for example to close an open connection to a database.<br>
325
+
Triggered when `fastify.close()` is invoked to stop the server. It is useful when [plugins](Plugins.md) need a "shutdown" event, for example to close an open connection to a database.<br>
326
326
The first argument is the Fastify instance, the second one the `done` callback.
Except for [onClose](#onclose), all hooks are encapsulated. This means that you can decide where your hooks should run by using `register` as explained in the [plugins guide](./Plugins-Guide.md). If you pass a function, that function is bound to the right Fastify context and from there you have full access to the Fastify API.
399
+
Except for [onClose](#onclose), all hooks are encapsulated. This means that you can decide where your hooks should run by using `register` as explained in the [plugins guide](Plugins-Guide.md). If you pass a function, that function is bound to the right Fastify context and from there you have full access to the Fastify API.
400
400
401
401
```js
402
402
fastify.addHook('onRequest', function (request, reply, done) {
By default, fastify adds an id to every request for easier tracking. If the "request-id" header is present its value is used, otherwise a new incremental id is generated. See Fastify Factory [`requestIdHeader`](./Server.md#factory-request-id-header) and Fastify Factory [`genReqId`](./Server.md#gen-request-id) for customization options.
60
+
By default, fastify adds an id to every request for easier tracking. If the "request-id" header is present its value is used, otherwise a new incremental id is generated. See Fastify Factory [`requestIdHeader`](Server.md#factory-request-id-header) and Fastify Factory [`genReqId`](Server.md#gen-request-id) for customization options.
61
61
62
-
The default logger is configured with a set of standard serializers that serialize objects with `req`, `res`, and `err` properties. The object received by `req` is the Fastify [`Request`](./Request.md) object, while the object received by `res` is the Fastify [`Reply`](./Reply.md) object.
62
+
The default logger is configured with a set of standard serializers that serialize objects with `req`, `res`, and `err` properties. The object received by `req` is the Fastify [`Request`](Request.md) object, while the object received by `res` is the Fastify [`Reply`](Reply.md) object.
63
63
This behaviour can be customized by specifying custom serializers.
64
64
```js
65
65
constfastify=require('fastify')({
@@ -136,7 +136,7 @@ fastify.get('/', function (request, reply) {
136
136
})
137
137
```
138
138
139
-
*The logger instance for the current request is available in every part of the [lifecycle](./Lifecycle.md).*
139
+
*The logger instance for the current request is available in every part of the [lifecycle](Lifecycle.md).*
Remember that middleware can be encapsulated, this means that you can decide where your middleware should run by using `register` as explained in the [plugins guide](./Plugins-Guide.md).
26
+
Remember that middleware can be encapsulated, this means that you can decide where your middleware should run by using `register` as explained in the [plugins guide](Plugins-Guide.md).
27
27
28
-
Fastify middleware also do not expose the `send` method or other methods specific to the Fastify [Reply](./Reply.md#reply) instance. This is because Fastify wraps the incoming `req` and `res` Node instances using the [Request](./Request.md#request) and [Reply](./Reply.md#reply) objects internally, but this is done after the middleware phase. If you need to create middleware, you have to use the Node `req` and `res` instances. Otherwise, you can use the `preHandler` hook which already has the [Request](./Request.md#request) and [Reply](./Reply.md#reply) Fastify instances. For more information, see [Hooks](./Hooks.md#hooks).
28
+
Fastify middleware also do not expose the `send` method or other methods specific to the Fastify [Reply](Reply.md#reply) instance. This is because Fastify wraps the incoming `req` and `res` Node instances using the [Request](Request.md#request) and [Reply](Reply.md#reply) objects internally, but this is done after the middleware phase. If you need to create middleware, you have to use the Node `req` and `res` instances. Otherwise, you can use the `preHandler` hook which already has the [Request](Request.md#request) and [Reply](Reply.md#reply) Fastify instances. For more information, see [Hooks](Hooks.md#hooks).
29
29
30
30
<aname="restrict-usage"></a>
31
31
#### Restrict middleware execution to a certain path(s)
0 commit comments