Fastify-based HTTP adapter for the NestJS framework, enabling high-performance HTTP server integration with NestJS applications
npx @tessl/cli install tessl/npm-nestjs--platform-fastify@11.1.0The 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.
npm install @nestjs/platform-fastifyimport { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';For individual components:
import {
FastifyAdapter,
NestFastifyApplication,
RouteConfig,
RouteConstraints,
RouteSchema,
FASTIFY_ROUTE_CONFIG_METADATA,
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
FASTIFY_ROUTE_SCHEMA_METADATA
} from '@nestjs/platform-fastify';import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import * as path from 'path';
async function bootstrap() {
try {
// Create NestJS application with Fastify adapter
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
// Enable CORS if needed
app.enableCors();
// Serve static assets
app.useStaticAssets({
root: path.join(__dirname, '..', 'public'),
prefix: '/public/',
});
// Listen on port 3000
await app.listen(3000);
console.log('Application is running on: http://localhost:3000');
} catch (error) {
console.error('Error starting the application:', error);
process.exit(1);
}
}
bootstrap();The NestJS Platform Fastify package is built around several key components:
Core adapter functionality for integrating Fastify with NestJS applications. Provides lifecycle management, middleware support, and request/response handling.
class FastifyAdapter<
TServer extends RawServerBase = RawServerDefault,
TRawRequest extends FastifyRawRequest<TServer> = FastifyRawRequest<TServer>,
TRawResponse extends RawReplyDefaultExpression<TServer> = RawReplyDefaultExpression<TServer>,
TRequest extends FastifyRequest<RequestGenericInterface, TServer, TRawRequest> = FastifyRequest<RequestGenericInterface, TServer, TRawRequest>,
TReply extends FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse> = FastifyReply<RouteGenericInterface, TServer, TRawRequest, TRawResponse>,
TInstance extends FastifyInstance<TServer, TRawRequest, TRawResponse> = FastifyInstance<TServer, TRawRequest, TRawResponse>
> extends AbstractHttpAdapter<TServer, TRequest, TReply>
interface FastifyAdapterConstructorOptions<
Server extends RawServerBase = RawServerDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> extends FastifyServerOptions<Server, Logger> {
skipMiddie?: boolean;
}Specialized decorators for configuring Fastify-specific route behavior including constraints, schema validation, and configuration options.
function RouteConfig(config: any): MethodDecorator;
function RouteConstraints(config: RouteShorthandOptions['config']): MethodDecorator;
function RouteSchema(schema: FastifySchema): MethodDecorator;Enhanced interfaces for NestJS applications with Fastify-specific functionality and configuration options for static assets, view engines, and body parsing.
interface NestFastifyApplication<TServer extends RawServerBase = RawServerDefault>
extends INestApplication<TServer> {
getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>;
register<Options extends FastifyPluginOptions = any>(
plugin: FastifyPluginCallback<Options> | FastifyPluginAsync<Options> | Promise<{ default: FastifyPluginCallback<Options> }> | Promise<{ default: FastifyPluginAsync<Options> }>,
opts?: FastifyRegisterOptions<Options>
): Promise<FastifyInstance>;
useBodyParser<TServer extends RawServerBase = RawServerBase>(
type: string | string[] | RegExp,
options?: NestFastifyBodyParserOptions,
parser?: FastifyBodyParser<Buffer, TServer>
): this;
useStaticAssets(options: FastifyStaticOptions): this;
enableCors(options?: FastifyCorsOptions): void;
setViewEngine(options: FastifyViewOptions | string): this;
}type NestFastifyBodyParserOptions = Omit<Parameters<AddContentTypeParser>[1], 'parseAs'>;
// Metadata constants for decorators
const FASTIFY_ROUTE_CONFIG_METADATA: string;
const FASTIFY_ROUTE_CONSTRAINTS_METADATA: string;
const FASTIFY_ROUTE_SCHEMA_METADATA: string;The adapter supports various Fastify server configurations:
type FastifyHttp2SecureOptions<
Server extends http2.Http2SecureServer,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
http2: true;
https: http2.SecureServerOptions;
};
type FastifyHttp2Options<
Server extends http2.Http2Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
http2: true;
http2SessionTimeout?: number;
};
type FastifyHttpsOptions<
Server extends https.Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
https: https.ServerOptions;
};
type FastifyHttpOptions<
Server extends http.Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyAdapterBaseOptions<Server, Logger> & {
http: http.ServerOptions;
};