0
# NestJS Platform Fastify
1
2
The NestJS Platform Fastify package provides a Fastify-based HTTP adapter for the NestJS framework, enabling developers to leverage Fastify's high-performance HTTP server as an alternative to Express. It offers seamless integration with NestJS applications through adapters, decorators, and interfaces while maintaining the framework's architectural patterns and providing access to Fastify's superior performance characteristics.
3
4
## Package Information
5
6
- **Package Name**: @nestjs/platform-fastify
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @nestjs/platform-fastify`
10
11
## Core Imports
12
13
```typescript
14
import { NestFactory } from '@nestjs/core';
15
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
16
```
17
18
For individual components:
19
20
```typescript
21
import {
22
FastifyAdapter,
23
NestFastifyApplication,
24
RouteConfig,
25
RouteConstraints,
26
RouteSchema,
27
FASTIFY_ROUTE_CONFIG_METADATA,
28
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
29
FASTIFY_ROUTE_SCHEMA_METADATA
30
} from '@nestjs/platform-fastify';
31
```
32
33
## Basic Usage
34
35
```typescript
36
import { NestFactory } from '@nestjs/core';
37
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
38
import { AppModule } from './app.module';
39
import * as path from 'path';
40
41
async function bootstrap() {
42
try {
43
// Create NestJS application with Fastify adapter
44
const app = await NestFactory.create<NestFastifyApplication>(
45
AppModule,
46
new FastifyAdapter()
47
);
48
49
// Enable CORS if needed
50
app.enableCors();
51
52
// Serve static assets
53
app.useStaticAssets({
54
root: path.join(__dirname, '..', 'public'),
55
prefix: '/public/',
56
});
57
58
// Listen on port 3000
59
await app.listen(3000);
60
console.log('Application is running on: http://localhost:3000');
61
} catch (error) {
62
console.error('Error starting the application:', error);
63
process.exit(1);
64
}
65
}
66
bootstrap();
67
```
68
69
## Architecture
70
71
The NestJS Platform Fastify package is built around several key components:
72
73
- **FastifyAdapter**: Core HTTP adapter that bridges NestJS and Fastify, handling request/response lifecycle
74
- **Route Decorators**: Specialized decorators for Fastify-specific route configuration, constraints, and schema validation
75
- **Application Interface**: Enhanced NestJS application interface with Fastify-specific methods
76
- **Type Integration**: Full TypeScript support with generic server, request, and response types
77
- **Plugin System**: Direct access to Fastify's plugin ecosystem through the adapter
78
79
## Capabilities
80
81
### HTTP Adapter Integration
82
83
Core adapter functionality for integrating Fastify with NestJS applications. Provides lifecycle management, middleware support, and request/response handling.
84
85
```typescript { .api }
86
class FastifyAdapter<
87
TServer extends RawServerBase = RawServerDefault,
88
TRawRequest extends FastifyRawRequest<TServer> = FastifyRawRequest<TServer>,
89
TRawResponse extends RawReplyDefaultExpression<TServer> = RawReplyDefaultExpression<TServer>,
90
TRequest extends FastifyRequest<RequestGenericInterface, TServer, TRawRequest> = FastifyRequest<RequestGenericInterface, TServer, TRawRequest>,
91
TReply extends FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse> = FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse>,
92
TInstance extends FastifyInstance<TServer, TRawRequest, TRawResponse> = FastifyInstance<TServer, TRawRequest, TRawResponse>
93
> extends AbstractHttpAdapter<TServer, TRequest, TReply>
94
95
interface FastifyAdapterConstructorOptions<
96
Server extends RawServerBase = RawServerDefault,
97
Logger extends FastifyBaseLogger = FastifyBaseLogger
98
> extends FastifyServerOptions<Server, Logger> {
99
skipMiddie?: boolean;
100
}
101
```
102
103
[HTTP Adapter](./fastify-adapter.md)
104
105
### Route Configuration Decorators
106
107
Specialized decorators for configuring Fastify-specific route behavior including constraints, schema validation, and configuration options.
108
109
```typescript { .api }
110
function RouteConfig(config: any): MethodDecorator;
111
function RouteConstraints(config: RouteShorthandOptions['config']): MethodDecorator;
112
function RouteSchema(schema: FastifySchema): MethodDecorator;
113
```
114
115
[Route Decorators](./decorators.md)
116
117
### Application and Configuration Interfaces
118
119
Enhanced interfaces for NestJS applications with Fastify-specific functionality and configuration options for static assets, view engines, and body parsing.
120
121
```typescript { .api }
122
interface NestFastifyApplication<TServer extends RawServerBase = RawServerDefault>
123
extends INestApplication<TServer> {
124
getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>;
125
register<Options extends FastifyPluginOptions = any>(
126
plugin: FastifyPluginCallback<Options> | FastifyPluginAsync<Options> | Promise<{ default: FastifyPluginCallback<Options> }> | Promise<{ default: FastifyPluginAsync<Options> }>,
127
opts?: FastifyRegisterOptions<Options>
128
): Promise<FastifyInstance>;
129
useBodyParser<TServer extends RawServerBase = RawServerBase>(
130
type: string | string[] | RegExp,
131
options?: NestFastifyBodyParserOptions,
132
parser?: FastifyBodyParser<Buffer, TServer>
133
): this;
134
useStaticAssets(options: FastifyStaticOptions): this;
135
enableCors(options?: FastifyCorsOptions): void;
136
setViewEngine(options: FastifyViewOptions | string): this;
137
}
138
```
139
140
[Interfaces and Configuration](./interfaces.md)
141
142
## Types
143
144
### Core Types
145
146
```typescript { .api }
147
type NestFastifyBodyParserOptions = Omit<Parameters<AddContentTypeParser>[1], 'parseAs'>;
148
149
// Metadata constants for decorators
150
const FASTIFY_ROUTE_CONFIG_METADATA: string;
151
const FASTIFY_ROUTE_CONSTRAINTS_METADATA: string;
152
const FASTIFY_ROUTE_SCHEMA_METADATA: string;
153
```
154
155
### Server Type Options
156
157
The adapter supports various Fastify server configurations:
158
159
```typescript { .api }
160
type FastifyHttp2SecureOptions<
161
Server extends http2.Http2SecureServer,
162
Logger extends FastifyBaseLogger = FastifyBaseLogger
163
> = FastifyAdapterBaseOptions<Server, Logger> & {
164
http2: true;
165
https: http2.SecureServerOptions;
166
};
167
168
type FastifyHttp2Options<
169
Server extends http2.Http2Server,
170
Logger extends FastifyBaseLogger = FastifyBaseLogger
171
> = FastifyAdapterBaseOptions<Server, Logger> & {
172
http2: true;
173
http2SessionTimeout?: number;
174
};
175
176
type FastifyHttpsOptions<
177
Server extends https.Server,
178
Logger extends FastifyBaseLogger = FastifyBaseLogger
179
> = FastifyAdapterBaseOptions<Server, Logger> & {
180
https: https.ServerOptions;
181
};
182
183
type FastifyHttpOptions<
184
Server extends http.Server,
185
Logger extends FastifyBaseLogger = FastifyBaseLogger
186
> = FastifyAdapterBaseOptions<Server, Logger> & {
187
http: http.ServerOptions;
188
};
189
```