INestApplication

Interface defining the core NestApplication object.


interface INestApplication extends INestApplicationContext {
  use(...args: any[]): this
  enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void
  enableVersioning(options?: VersioningOptions): this
  listen(port: string | number, callback?: () => void): Promise<any>
  getUrl(): Promise<string>
  setGlobalPrefix(prefix: string, options?: GlobalPrefixOptions<string | RouteInfo>): this
  useWebSocketAdapter(adapter: WebSocketAdapter<any, any, any>): this
  connectMicroservice<T extends object = any>(options: T, hybridOptions?: NestHybridApplicationOptions): INestMicroservice
  getMicroservices(): INestMicroservice[]
  getHttpServer(): any
  getHttpAdapter(): HttpServer
  startAllMicroservices(): Promise<this>
  useGlobalFilters(...filters: ExceptionFilter<any>[]): this
  useGlobalPipes(...pipes: PipeTransform<any, any>[]): this
  useGlobalInterceptors(...interceptors: NestInterceptor<any, any>[]): this
  useGlobalGuards(...guards: CanActivate[]): this
  close(): Promise<void>

  // inherited from nest/packages/common/INestApplicationContext
  select<T>(module: Type<T> | DynamicModule): INestApplicationContext
  get<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>): TResult
  resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>): Promise<TResult>
  registerRequestByContextId<T = any>(request: T, contextId: { id: number; }): void
  close(): Promise<void>
  useLogger(logger: false | LoggerService | LogLevel[]): void
  flushLogs(): void
  enableShutdownHooks(signals?: string[] | ShutdownSignal[]): this
  init(): Promise<this>
}

Methods

use()

A wrapper function around HTTP adapter method: adapter.use(). Example app.use(cors())


use(...args: any[]): this

Parameters

Option Type Description
args any[]

Returns

this

enableCors()

Enables CORS (Cross-Origin Resource Sharing)


enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void

Parameters

Option Type Description
options CorsOptions | CorsOptionsDelegate

Optional. Default is undefined.

Returns

void

enableVersioning()

Enables Versioning for the application. By default, URI-based versioning is used.


enableVersioning(options?: VersioningOptions): this

Parameters

Option Type Description
options VersioningOptions

Optional. Default is undefined.

Returns

this

listen()

Starts the application.


listen(port: string | number, hostname: string, callback?: () => void): Promise<any>

Parameters

Option Type Description
port string | number
hostname string
callback () => void

Optional. Default is undefined.

Returns

Promise<any>

getUrl()

Returns the url the application is listening at, based on OS and IP version. Returns as an IP value either in IPv6 or IPv4


getUrl(): Promise<string>

Parameters

There are no parameters.

Returns

Promise<string> The IP where the server is listening

setGlobalPrefix()

Registers a prefix for every HTTP route path.


setGlobalPrefix(prefix: string, options?: GlobalPrefixOptions<string | RouteInfo>): this

Parameters

Option Type Description
prefix string

The prefix for every HTTP route path (for example /v1/api)

options GlobalPrefixOptions

Global prefix options object


Optional. Default is `undefined`.

Returns

this

useWebSocketAdapter()

Register Ws Adapter which will be used inside Gateways. Use when you want to override default socket.io library.


useWebSocketAdapter(adapter: WebSocketAdapter<any, any, any>): this

Parameters

Option Type Description
adapter WebSocketAdapter

Returns

this

connectMicroservice()

Connects microservice to the NestApplication instance. Transforms application to a hybrid instance.


connectMicroservice<T extends object = any>(options: T, hybridOptions?: NestHybridApplicationOptions): INestMicroservice

Parameters

Option Type Description
options T

Microservice options object

hybridOptions NestHybridApplicationOptions

Hybrid options object


Optional. Default is `undefined`.

Returns

INestMicroservice

getMicroservices()

Returns array of the microservices connected to the NestApplication.


getMicroservices(): INestMicroservice[]

Parameters

There are no parameters.

Returns

INestMicroservice[]

getHttpServer()

Returns the underlying native HTTP server.


getHttpServer(): any

Parameters

There are no parameters.

Returns

any

getHttpAdapter()

Returns the underlying HTTP adapter.


getHttpAdapter(): HttpServer

Parameters

There are no parameters.

Returns

HttpServer

startAllMicroservices()

Starts all connected microservices asynchronously.


startAllMicroservices(): Promise<this>

Parameters

There are no parameters.

Returns

Promise<this>

useGlobalFilters()

Registers exception filters as global filters (will be used within every HTTP route handler)


useGlobalFilters(...filters: ExceptionFilter<any>[]): this

Parameters

Option Type Description
filters ExceptionFilter

Returns

this

useGlobalPipes()

Registers pipes as global pipes (will be used within every HTTP route handler)


useGlobalPipes(...pipes: PipeTransform<any, any>[]): this

Parameters

Option Type Description
pipes PipeTransform

Returns

this

useGlobalInterceptors()

Registers interceptors as global interceptors (will be used within every HTTP route handler)


useGlobalInterceptors(...interceptors: NestInterceptor<any, any>[]): this

Parameters

Option Type Description
interceptors NestInterceptor

Returns

this

useGlobalGuards()

Registers guards as global guards (will be used within every HTTP route handler)


useGlobalGuards(...guards: CanActivate[]): this

Parameters

Option Type Description
guards CanActivate

Returns

this

close()

Terminates the application (including NestApplication, Gateways, and each connected microservice)


close(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>