Skip to content

NestApplication

reference
2 min readUpdated

Kind: Class

Source: packages/core/nest-application.ts

Part of: Core

NestApplication is the core runtime class behind a Nest HTTP application. It coordinates HTTP server creation, module registration, WebSocket integration, parser middleware, and application lifecycle initialization. Applications are typically created through NestFactory, which configures and returns a NestApplication instance.

Extends: NestApplicationContext

Implements: INestApplication

Methods

MethodSignatureReturns
disposedispose()Promise<void>
getHttpAdaptergetHttpAdapter()AbstractHttpAdapter
registerHttpServerregisterHttpServer()void
getUnderlyingHttpServergetUnderlyingHttpServer()T
applyOptionsapplyOptions()void
createServercreateServer()T
registerModulesregisterModules()void
registerWsModuleregisterWsModule()void
initinit()Promise<this>
registerParserMiddlewareregisterParserMiddleware()void
registerRouterregisterRouter()void
registerRouterHooksregisterRouterHooks()void
connectMicroserviceconnectMicroservice(microserviceOptions: T, hybridAppOptions: NestHybridApplicationOptions)INestMicroservice
getMicroservicesgetMicroservices()INestMicroservice[]
getHttpServergetHttpServer()void
startAllMicroservicesstartAllMicroservices()Promise<this>
useuse(args: [any, any?])this
useBodyParseruseBodyParser(args: [any, any?])this
enableCorsenableCors(options: any)void
enableVersioningenableVersioning(options: VersioningOptions)this
listen`listen(port: numberstring)`
listen`listen(port: numberstring, hostname: string)`
listen`listen(port: numberstring, args: any[])`
getUrlgetUrl()Promise<string>
setGlobalPrefixsetGlobalPrefix(prefix: string, options: GlobalPrefixOptions)this
useWebSocketAdapteruseWebSocketAdapter(adapter: WebSocketAdapter)this
useGlobalFiltersuseGlobalFilters(filters: ExceptionFilter[])this
useGlobalPipesuseGlobalPipes(pipes: PipeTransform<any>[])this
useGlobalInterceptorsuseGlobalInterceptors(interceptors: NestInterceptor[])this
useGlobalGuardsuseGlobalGuards(guards: CanActivate[])this
useStaticAssetsuseStaticAssets(options: any)this
useStaticAssetsuseStaticAssets(path: string, options: any)this
useStaticAssetsuseStaticAssets(pathOrOptions: any, options: any)this
setBaseViewsDir`setBaseViewsDir(path: stringstring[])`
setViewEnginesetViewEngine(engineOrOptions: any)this

Properties

PropertyType
loggerany

Where it refuses work

  • NestApplication stops the work with an early return when !this.appOptions || !this.appOptions.cors.
  • NestApplication stops the work with an early return when !passCustomOptions.
  • NestApplication stops the work with an early return when !this.socketModule.
  • NestApplication stops the work with an early return when this.isInitialized.
  • NestApplication stops the work with an early return when originalCallbackArgs[0] instanceof Error.
  • NestApplication stops the work with an early return when platform() === 'win32'.

Diagram

mermaid
graph LR
  A[NestFactory.create()] --> B[NestApplication]
  B --> C[createServer()]
  B --> D[registerModules()]
  B --> E[registerWsModule()]
  B --> F[registerParserMiddleware()]
  B --> G[init()]
  G --> H[HTTP Adapter]
  H --> I[Underlying HTTP Server]
  B --> J[dispose()]

Usage

ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Initialize registered modules, middleware, and adapters.
  await app.init();

  const httpAdapter = app.getHttpAdapter();
  const server = app.getUnderlyingHttpServer();

  console.log(`Using adapter: ${httpAdapter.getType()}`);
  console.log(`Server available: ${Boolean(server)}`);

  await app.listen(3000);

  process.on('SIGTERM', async () => {
    await app.close();
    await app.dispose();
  });
}

bootstrap();

AI Coding Instructions

  • Create application instances through NestFactory.create() rather than constructing NestApplication directly.
  • Preserve the initialization order: register modules and infrastructure before calling init().
  • Use getHttpAdapter() for adapter-specific integrations; avoid assuming Express or Fastify APIs are always available.
  • Use getUnderlyingHttpServer() only when direct access to the native HTTP server is required.
  • Ensure shutdown paths clean up resources through the application lifecycle, including disposal when appropriate.

Relationships

  • IMPORTS → CanActivate
  • IMPORTS → ExceptionFilter
  • IMPORTS → HttpServer
  • IMPORTS → INestApplication
  • IMPORTS → INestMicroservice
  • IMPORTS → NestHybridApplicationOptions
  • IMPORTS → NestInterceptor
  • IMPORTS → PipeTransform
  • IMPORTS → VersioningOptions
  • IMPORTS → VersioningType
  • IMPORTS → WebSocketAdapter
  • IMPORTS → GlobalPrefixOptions
  • IMPORTS → NestApplicationOptions
  • IMPORTS → Logger
  • IMPORTS → loadPackage
  • IMPORTS → addLeadingSlash
  • IMPORTS → isFunction
  • IMPORTS → isObject
  • IMPORTS → isString
  • IMPORTS → SocketModule
  • IMPORTS → MicroservicesModule
  • IMPORTS → -nestjs-microservices

Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (2)

  • TestingModulepackages/testing/testing-module.ts:26
  • BaseWsInstancepackages/websockets/adapters/ws-adapter.ts:8

Was this page helpful?

Download as PDF
NestApplication — NestJS head-to-head