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
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
To access the raw request body in a controller, a convenience interface `RawBodyRequest` is provided to expose a `rawBody` field on the request: use the interface `RawBodyRequest` type:
Copy file name to clipboardExpand all lines: content/middlewares.md
+6-4
Original file line number
Diff line number
Diff line change
@@ -20,8 +20,7 @@ Nest middleware are, by default, equivalent to [express](https://expressjs.com/e
20
20
21
21
You implement custom Nest middleware in either a function, or in a class with an `@Injectable()` decorator. The class should implement the `NestMiddleware` interface, while the function does not have any special requirements. Let's start by implementing a simple middleware feature using the class method.
22
22
23
-
> warning **Warning**`Express` and `fastify` handle middleware differently and provide different method signatures, read more [here](/techniques/performance#middleware).
24
-
23
+
> warning **Warning**`Express` and `fastify` handle middleware differently and provide different method signatures, read more [here](/techniques/performance#middleware).
25
24
26
25
```typescript
27
26
@@filename(logger.middleware)
@@ -132,7 +131,10 @@ export class AppModule {
132
131
Pattern based routes are supported as well. For instance, the asterisk is used as a **wildcard**, and will match any combination of characters:
The `'ab*cd'` route path will match `abcd`, `ab_cd`, `abecd`, and so on. The characters `?`, `+`, `*`, and `()` may be used in a route path, and are subsets of their regular expression counterparts. The hyphen ( `-`) and the dot (`.`) are interpreted literally by string-based paths.
@@ -245,7 +247,7 @@ If we want to bind middleware to every registered route at once, we can use the
245
247
@@filename(main)
246
248
const app =awaitNestFactory.create(AppModule);
247
249
app.use(logger);
248
-
awaitapp.listen(3000);
250
+
awaitapp.listen(process.env.PORT??3000);
249
251
```
250
252
251
253
> info **Hint** Accessing the DI container in a global middleware is not possible. You can use a [functional middleware](middleware#functional-middleware) instead when using `app.use()`. Alternatively, you can use a class middleware and consume it with `.forRoutes('*')` within the `AppModule` (or any other module).
@@ -62,14 +62,15 @@ As you can see, the `SwaggerModule` automatically reflects all of your endpoints
62
62
63
63
> info **Hint** To generate and download a Swagger JSON file, navigate to `http://localhost:3000/api-json` (assuming that your Swagger documentation is available under `http://localhost:3000/api`).
64
64
> It is also possible to expose it on a route of your choice using only the setup method from `@nestjs/swagger`, like this:
65
+
>
65
66
> ```typescript
66
67
>SwaggerModule.setup('swagger', app, document, {
67
68
> jsonDocumentUrl: 'swagger/json',
68
69
> });
69
70
>```
71
+
>
70
72
> Which would expose it at `http://localhost:3000/swagger/json`
71
73
72
-
73
74
>warning**Warning**Whenusing`fastify`and`helmet`, theremaybeaproblemwith [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP), to solve this collision, configure the CSP as shown below:
Copy file name to clipboardExpand all lines: content/security/cors.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ To enable CORS, call the `enableCors()` method on the Nest application object.
9
9
```typescript
10
10
const app =awaitNestFactory.create(AppModule);
11
11
app.enableCors();
12
-
awaitapp.listen(3000);
12
+
awaitapp.listen(process.env.PORT??3000);
13
13
```
14
14
15
15
The `enableCors()` method takes an optional configuration object argument. The available properties of this object are described in the official [CORS](https://github.com/expressjs/cors#configuration-options) documentation. Another way is to pass a [callback function](https://github.com/expressjs/cors#configuring-cors-asynchronously) that lets you define the configuration object asynchronously based on the request (on the fly).
@@ -19,5 +19,5 @@ Or, pass a [CORS configuration object](https://github.com/expressjs/cors#configu
Copy file name to clipboardExpand all lines: content/techniques/logger.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ To disable logging, set the `logger` property to `false` in the (optional) Nest
21
21
const app =awaitNestFactory.create(AppModule, {
22
22
logger: false,
23
23
});
24
-
awaitapp.listen(3000);
24
+
awaitapp.listen(process.env.PORT??3000);
25
25
```
26
26
27
27
To enable specific logging levels, set the `logger` property to an array of strings specifying the log levels to display, as follows:
@@ -30,7 +30,7 @@ To enable specific logging levels, set the `logger` property to an array of stri
30
30
const app =awaitNestFactory.create(AppModule, {
31
31
logger: ['error', 'warn'],
32
32
});
33
-
awaitapp.listen(3000);
33
+
awaitapp.listen(process.env.PORT??3000);
34
34
```
35
35
36
36
Values in the array can be any combination of `'log'`, `'fatal'`, `'error'`, `'warn'`, `'debug'`, and `'verbose'`.
@@ -45,7 +45,7 @@ You can provide a custom logger implementation to be used by Nest for system log
45
45
const app =awaitNestFactory.create(AppModule, {
46
46
logger: console,
47
47
});
48
-
awaitapp.listen(3000);
48
+
awaitapp.listen(process.env.PORT??3000);
49
49
```
50
50
51
51
Implementing your own custom logger is straightforward. Simply implement each of the methods of the `LoggerService` interface as shown below.
@@ -93,7 +93,7 @@ You can then supply an instance of `MyLogger` via the `logger` property of the N
93
93
const app =awaitNestFactory.create(AppModule, {
94
94
logger: newMyLogger(),
95
95
});
96
-
awaitapp.listen(3000);
96
+
awaitapp.listen(process.env.PORT??3000);
97
97
```
98
98
99
99
This technique, while simple, doesn't utilize dependency injection for the `MyLogger` class. This can pose some challenges, particularly for testing, and limit the reusability of `MyLogger`. For a better solution, see the <ahref="techniques/logger#dependency-injection">Dependency Injection</a> section below.
> info **Note** In the example above, we set the `bufferLogs` to `true` to make sure all logs will be buffered until a custom logger is attached (`MyLogger` in this case) and the application initialisation process either completes or fails. If the initialisation process fails, Nest will fallback to the original `ConsoleLogger` to print out any reported error messages. Also, you can set the `autoFlushLogs` to `false` (default `true`) to manually flush logs (using the `Logger#flush()` method).
> info **Hint** Alternatively, instead of setting `bufferLogs` to `true`, you could temporarily disable the logger with `logger: false` instruction. Be mindful that if you supply `logger: false` to `NestFactory.create`, nothing will be logged until you call `useLogger`, so you may miss some important initialization errors. If you don't mind that some of your initial messages will be logged with the default logger, you can just omit the `logger: false` option.
0 commit comments