NestFastifyApplication


interface NestFastifyApplication<TServer extends RawServerBase = RawServerDefault> extends INestApplication {
  getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>
  register<Options extends FastifyPluginOptions = any>(plugin: any, opts?: FastifyRegisterOptions<Options>): Promise<FastifyInstance>
  useBodyParser<TServer extends RawServerBase = RawServerBase>(type: string | RegExp | string[], options?: NestFastifyBodyParserOptions, parser?: FastifyBodyParser<Buffer, TServer>): this
  useStaticAssets(options: FastifyStaticOptions): this
  enableCors(options?: FastifyCorsOptions): void
  setViewEngine(options: string | FastifyViewOptions): this
  inject(): LightMyRequestChain
  listen(opts: FastifyListenOptions, callback?: (err: Error, address: string) => void): Promise<TServer>
}

Methods

getHttpAdapter()

Returns the underlying HTTP adapter bounded to a Fastify app.


getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>

Parameters

There are no parameters.

Returns

HttpServer<FastifyRequest, FastifyReply, FastifyInstance>

register()

A wrapper function around native fastify.register() method. Example `app.register(require('@fastify/formbody'))


register<Options extends FastifyPluginOptions = any>(plugin: any, opts?: FastifyRegisterOptions<Options>): Promise<FastifyInstance>

Parameters

Option Type Description
plugin any
opts FastifyRegisterOptions

Optional. Default is undefined.

Returns

Promise<FastifyInstance>

useBodyParser()

Register Fastify body parsers on the fly. Will respect the application's rawBody option.


useBodyParser<TServer extends RawServerBase = RawServerBase>(type: string | RegExp | string[], options?: NestFastifyBodyParserOptions, parser?: FastifyBodyParser<Buffer, TServer>): this

Parameters

Option Type Description
type string | RegExp | string[]
options NestFastifyBodyParserOptions

Optional. Default is undefined.

parser FastifyBodyParser

Optional. Default is undefined.

Examples

    
const app = await NestFactory.create(
  AppModule,
  new FastifyAdapter(),
  { rawBody: true }
);
// enable the json parser with a parser limit of 50mb
app.useBodyParser('application/json', { bodyLimit: 50 * 1000 * 1024 });

Returns

this

useStaticAssets()

Sets a base directory for public assets. Example app.useStaticAssets({ root: 'public' })


useStaticAssets(options: FastifyStaticOptions): this

Parameters

Option Type Description
options FastifyStaticOptions

Returns

this

enableCors()

Enables CORS (Cross-Origin Resource Sharing)


enableCors(options?: FastifyCorsOptions): void

Parameters

Option Type Description
options FastifyCorsOptions

Optional. Default is undefined.

Returns

void

setViewEngine()

Sets a view engine for templates (views), for example: pug, handlebars, or ejs.


setViewEngine(options: string | FastifyViewOptions): this

Parameters

Option Type Description
options string | FastifyViewOptions

Returns

this

Don't pass in a string. The string type in the argument is for compatibility reason and will cause an exception.

inject()

A wrapper function around native fastify.inject() method.


inject(opts: any): Promise<LightMyRequestResponse>

Parameters

Option Type Description
opts any

Returns

Promise<LightMyRequestResponse>

listen()

Starts the application.

Overload #1

listen(opts?: FastifyListenOptions): Promise<TServer>

Parameters

Option Type Description
opts FastifyListenOptions

Optional. Default is undefined.

Returns

Promise<TServer>


Overload #2

listen(callback?: (err: Error, address: string) => void): Promise<TServer>

Parameters

Option Type Description
callback (err: Error, address: string) => void

Optional. Default is undefined.

Returns

Promise<TServer>


Overload #3

listen(port: string | number, callback?: (err: Error, address: string) => void): Promise<TServer>

Parameters

Option Type Description
port string | number
callback (err: Error, address: string) => void

Optional. Default is undefined.

Returns

Promise<TServer>


Overload #4

listen(port: string | number, address: string, callback?: (err: Error, address: string) => void): Promise<TServer>

Parameters

Option Type Description
port string | number
address string
callback (err: Error, address: string) => void

Optional. Default is undefined.

Returns

Promise<TServer>


Overload #5

listen(port: string | number, address: string, backlog: number, callback?: (err: Error, address: string) => void): Promise<TServer>

Parameters

Option Type Description
port string | number
address string
backlog number
callback (err: Error, address: string) => void

Optional. Default is undefined.

Returns

Promise<TServer>