Skip to main content

@nestjs-mod/fastify

Application modules with Fastify adapter for NestJS-mod

NPM version monthly downloads Telegram Discord

Installation

npm i --save fastify @nestjs/platform-fastify @nestjs-mod/fastify

Modules

LinkCategoryDescription
FastifyNestApplicationInitializersystemFastify NestJS application initializer.
FastifyNestApplicationListenersystemFastify NestJS application listener.

Modules descriptions

FastifyNestApplicationInitializer

Fastify NestJS application initializer.

Use in NestJS-mod

Use without options.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { FastifyNestApplicationInitializer } from '@nestjs-mod/fastify';

bootstrapNestApplication({
modules: {
system: [FastifyNestApplicationInitializer.forRoot()],
},
});

Example of register fastify plugin.

import { bootstrapNestApplication } from '@nestjs-mod/common';
import { FastifyNestApplicationInitializer } from '@nestjs-mod/fastify';
import * as fmp from 'fastify-multipart';

bootstrapNestApplication({
modules: {
system: [
FastifyNestApplicationInitializer.forRoot({
staticConfiguration: {
wrapFastifyAdapter: (fastifyAdapter: FastifyAdapter) => {
fastifyAdapter.register(fmp);
},
},
}),
],
},
});

Static configuration

KeyDescriptionConstraintsDefaultValue
corsCORS options from CORS packageoptional--
bodyParserWhether to use underlying platform body parser.optional--
httpsOptionsSet of configurable HTTPS optionsoptional--
rawBodyWhether to register the raw request body on the request. Use req.rawBody.optional--
defaultLoggerFastify logger for applicationoptional--
loggerSpecifies the logger to use. Pass false to turn off logging.optional--
abortOnErrorWhether to abort the process on Error. By default, the process is exited. Pass false to override the default behavior. If false is passed, Nest will not exit the application and instead will rethrow the exception. @default trueoptional--
bufferLogsIf enabled, logs will be buffered until the "Logger#flush" method is called. @default falseoptional--
autoFlushLogsIf enabled, logs will be automatically flushed and buffer detached when application initialization process either completes or fails. @default trueoptional--
previewWhether to run application in the preview mode. In the preview mode, providers/controllers are not instantiated & resolved. @default falseoptional--
snapshotWhether to generate a serialized graph snapshot. @default falseoptional--
forceCloseConnectionsForce close open HTTP connections. Useful if restarting your application hangs due to keep-alive connections in the HTTP adapter.optionaltrue-
wrapFastifyAdapterMethod for additional actions before listeningoptional--

Back to Top


FastifyNestApplicationListener

Fastify NestJS application listener.

Use in NestJS-mod

Use with manual environments and custom configuration.

import { bootstrapNestApplication, isInfrastructureMode } from '@nestjs-mod/common';
import { FastifyNestApplicationInitializer, FastifyNestApplicationListener } from '@nestjs-mod/fastify';
import { Logger } from '@nestjs/common';

bootstrapNestApplication({
modules: {
system: [
FastifyNestApplicationInitializer.forRoot(),
FastifyNestApplicationListener.forRoot({
staticEnvironments: { port: 3000 },
staticConfiguration: {
mode: isInfrastructureMode() ? 'silent' : 'listen',
preListen: async ({ app }) => {
if (app) {
app.setGlobalPrefix('api');
}
},
postListen: async ({ current }) => {
Logger.log(
`🚀 Application is running on: http://${current.staticEnvironments?.hostname || 'localhost'}:${
current.staticEnvironments?.port
}/api`
);
},
},
}),
],
},
});

Static environments

KeyDescriptionSourcesConstraintsDefaultValue
portThe port on which to run the server.obj['port'], process.env['PORT']optional30003000
hostnameHostname on which to listen for incoming packets.obj['hostname'], process.env['HOSTNAME']optional0.0.0.00.0.0.0

Static configuration

KeyDescriptionConstraintsDefaultValue
modeMode of start application: init - for run NestJS life cycle, listen - for full start NestJS applicationoptionallisten-
preListenMethod for additional actions before listeningoptional--
postListenMethod for additional actions after listeningoptional--
defaultLoggerFastify logger for applicationoptional--
enableShutdownHooksEnable shutdown hooksoptionaltrue-
globalPrefixGlobal prefixoptionalapi-
autoCloseTimeoutInInfrastructureModeTimeout seconds for automatically closes the application in infrastructure mode if the application does not close itself (zero - disable)optional--
logApplicationStartLog application startoptionaltrue-

Back to Top

License

MIT