Skip to content

Commit 00e58a8

Browse files
Merge pull request #2655 from tolgap/use-body-parser
docs(raw-body): add examples and explainers for body parser limits with `app.useBodyParser`
2 parents 5b5adb6 + 7eb7733 commit 00e58a8

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

content/faq/raw-body.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,31 @@ By default, only `json` and `urlencoded` parsers are registered. If you want to
4242
For example, to register a `text` parser, you can use the following code:
4343

4444
```typescript
45-
app.useBodyParser('text')
45+
app.useBodyParser('text');
4646
```
4747

48+
> warning **Warning** Ensure that you are providing the correct application type to the `NestFactory.create` call. For Express applications, the correct type is `NestExpressApplication`. Otherwise the `.useBodyParser` method will not be found.
49+
50+
#### Body parser size limit
51+
52+
If your application needs to parse a body larger than the default `100kb` of Express, use the following:
53+
54+
```typescript
55+
app.useBodyParser('json', { limit: '10mb' });
56+
```
57+
58+
The `.useBodyParser` method will respect the `rawBody` option that is passed in the application options.
59+
4860
#### Use with Fastify
4961

5062
First enable the option when creating your Nest Fastify application:
5163

5264
```typescript
5365
import { NestFactory } from '@nestjs/core';
54-
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
66+
import {
67+
FastifyAdapter,
68+
NestFastifyApplication,
69+
} from '@nestjs/platform-fastify';
5570
import { AppModule } from './app.module';
5671

5772
// in the "bootstrap" function
@@ -60,7 +75,7 @@ const app = await NestFactory.create<NestFastifyApplication>(
6075
new FastifyAdapter(),
6176
{
6277
rawBody: true,
63-
}
78+
},
6479
);
6580
await app.listen(3000);
6681
```
@@ -87,5 +102,18 @@ By default, only `application/json` and `application/x-www-form-urlencoded` pars
87102
For example, to register a `text/plain` parser, you can use the following code:
88103

89104
```typescript
90-
app.useBodyParser('text/plain')
105+
app.useBodyParser('text/plain');
91106
```
107+
108+
> warning **Warning** Ensure that you are providing the correct application type to the `NestFactory.create` call. For Fastify applications, the correct type is `NestFastifyApplication`. Otherwise the `.useBodyParser` method will not be found.
109+
110+
#### Body parser size limit
111+
112+
If your application needs to parse a body larger than the default 1MiB of Fastify, use the following:
113+
114+
```typescript
115+
const bodyLimit = 10_485_760; // 10MiB
116+
app.useBodyParser('application/json', { bodyLimit });
117+
```
118+
119+
The `.useBodyParser` method will respect the `rawBody` option that is passed in the application options.

0 commit comments

Comments
 (0)