Skip to content

Commit 66779cc

Browse files
committed
docs(performance): added section middleware
1 parent cdda539 commit 66779cc

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

content/middlewares.md

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Nest middleware are, by default, equivalent to [express](https://expressjs.com/e
2020

2121
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.
2222

23-
> warning **Warning** `Express` and `fastify` treat middleware differently and have different syntaxes for middleware.
23+
> warning **Warning** `Express` and `fastify` treat middleware differently and have different syntaxes for middleware, for more information read [here](https://docs.nestjs.com/techniques/performance#middleware).
2424
2525
#### Use with Express
2626

@@ -48,36 +48,6 @@ export class LoggerMiddleware {
4848
}
4949
```
5050

51-
#### Use with Fastify
52-
53-
The middleware with `fastify` "works", but it retrieves only the raw `req` and `res` objects and not the fastify wrapper. This is a side effect of how `middie` works and `fastify` already talks about it in its documents for more information see [here](https://www.fastify.io/docs/latest/Reference/Middleware/).
54-
55-
> warning **Warning** The middleware in `fastify` has an IncomingMessage request type `FastifyRequest['raw']` and a ServerResponse type `FastifyReply['raw']`.
56-
57-
```typescript
58-
@@filename(logger.middleware)
59-
import { Injectable, NestMiddleware } from '@nestjs/common';
60-
import { FastifyRequest, FastifyReply } from 'fastify';
61-
62-
@Injectable()
63-
export class LoggerMiddleware implements NestMiddleware {
64-
use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) {
65-
console.log('Request...');
66-
next();
67-
}
68-
}
69-
@@switch
70-
import { Injectable } from '@nestjs/common';
71-
72-
@Injectable()
73-
export class LoggerMiddleware {
74-
use(req, res, next) {
75-
console.log('Request...');
76-
next();
77-
}
78-
}
79-
```
80-
8151
#### Dependency injection
8252

8353
Nest middleware fully supports Dependency Injection. Just as with providers and controllers, they are able to **inject dependencies** that are available within the same module. As usual, this is done through the `constructor`.

content/techniques/performance.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ You can pass options into the Fastify constructor through the `FastifyAdapter` c
7474
new FastifyAdapter({ logger: true });
7575
```
7676

77+
78+
#### Middleware
79+
80+
The middleware with `fastify` "works", but it retrieves only the raw `req` and `res` objects and not the fastify wrapper. This is a side effect of how `middie` works and `fastify` already talks about it in its documents for more information see [here](https://www.fastify.io/docs/latest/Reference/Middleware/).
81+
82+
> warning **Warning** The middleware in `fastify` has an IncomingMessage request type `FastifyRequest['raw']` and a ServerResponse type `FastifyReply['raw']`.
83+
84+
```typescript
85+
@@filename(logger.middleware)
86+
import { Injectable, NestMiddleware } from '@nestjs/common';
87+
import { FastifyRequest, FastifyReply } from 'fastify';
88+
89+
@Injectable()
90+
export class LoggerMiddleware implements NestMiddleware {
91+
use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) {
92+
console.log('Request...');
93+
next();
94+
}
95+
}
96+
@@switch
97+
import { Injectable } from '@nestjs/common';
98+
99+
@Injectable()
100+
export class LoggerMiddleware {
101+
use(req, res, next) {
102+
console.log('Request...');
103+
next();
104+
}
105+
}
106+
```
107+
108+
77109
#### Example
78110

79111
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/10-fastify).

0 commit comments

Comments
 (0)