HTTP adapter

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

Basically, every native (platform-specific) HTTP server/library instance is wrapped in an adapter. The adapter is registered as a globally available provider that might be plucked from the application context as well as injected into other providers easily.

Outside strategy

In order to get the HttpAdapter from the outside of the application context, you can call getHttpAdapter() method.

  1. @@filename()
  2. const app = await NestFactory.create(ApplicationModule);
  3. const httpAdapter = app.getHttpAdapter();

In-context strategy

In order to get the HttpAdapterHost from within the application context, you can inject it in the same way as any other existing provider (let’s say, through constructor).

  1. @@filename()
  2. export class CatsService {
  3. constructor(private readonly adapterHost: HttpAdapterHost) {}
  4. }
  5. @@switch
  6. @Dependencies(HttpAdapterHost)
  7. export class CatsService {
  8. constructor(adapterHost) {
  9. this.adapterHost = adapterHost;
  10. }
  11. }

info Hint The HttpAdapterHost is imported from the @nestjs/core package.

Adapter host

So far, we have learnt how to get the HttpAdapterHost. However, it’s still not an actual HttpAdapter. In order to get the HttpAdapter, simply access the httpAdapter property.

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

The httpAdapter is a real instance of the HTTP adapter used by the framework underneath. It can be either ExpressAdapter or FastifyAdapter (both classes extend AbstractHttpAdapter).

Every adapter exposes several useful methods to interact with the HTTP server. Nonetheless, if you want to access the library reference directly, call getInstance() method.

  1. const instance = httpAdapter.getInstance();