NestJS Logo

HTTP adapter

Occasionally, you may need to access the underlying HTTP server, either from within the Nest application context or from outside of it.

Every native (platform-specific) HTTP server or library instance (e.g., Express or Fastify) is wrapped in an adapter. The adapter is registered as a globally available provider, so you can retrieve it from the application context or inject it into other providers.

Outside application context strategy#

To get a reference to the HttpAdapter from outside of the application context, call the getHttpAdapter() method:

JS TS

const app = await NestFactory.create(AppModule);
const httpAdapter = app.getHttpAdapter();

As injectable#

To get a reference to the HttpAdapterHost from within the application context, inject it like any other provider (e.g., using constructor injection):

JS TS

export class CatsService {
  constructor(private adapterHost: HttpAdapterHost) {}
}

@Dependencies(HttpAdapterHost)
export class CatsService {
  constructor(adapterHost) {
    this.adapterHost = adapterHost;
  }
}
Hint The HttpAdapterHost is imported from the @nestjs/core package.

The HttpAdapterHost is not an actual HttpAdapter. To get the actual HttpAdapter instance, read its httpAdapter property:


const adapterHost = app.get(HttpAdapterHost);
const httpAdapter = adapterHost.httpAdapter;

The httpAdapter is the HTTP adapter instance that wraps the underlying framework. It is an instance of either ExpressAdapter or FastifyAdapter (both classes extend AbstractHttpAdapter).

The adapter object exposes several methods for interacting with the HTTP server. To access the library instance (e.g., the Express instance) directly, call the getInstance() method:


const instance = httpAdapter.getInstance();

Listening event#

To run code when the server starts listening for incoming requests, subscribe to the listen$ stream:


this.httpAdapterHost.listen$.subscribe(() =>
  console.log('HTTP server is listening'),
);

The HttpAdapterHost also exposes a listening boolean property that indicates whether the server is currently listening:


if (this.httpAdapterHost.listening) {
  console.log('HTTP server is listening');
}
Edit on GitHub

Support us

Nest is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you'd like to join them, please read more here.

Principal Sponsors

SerpApi LogoTrilon LogoMojam Logo

Sponsors / Partners

Become a sponsor