Niklas Higi
Niklas Higi

Reputation: 2309

How to inject HTTP server in Nest 6?

I'm correctly working on upgrading a project from Nest 6 and I can't figure out how to inject the currently used HTTP server instance into a class.

Previously, I've been using the HTTP_SERVER_REF from @nestjs/core like this:

@Inject(HTTP_SERVER_REF) private readonly httpServer: HttpServer

This constant doesn't seem to exist anymore. I have a few monkey-patch solutions in mind that would give me access to the HTTP server but I'm wondering: Is there a new, proper way to inject the HTTP server? I'm using the default @nestjs/platform-express package by the way.

Upvotes: 2

Views: 4665

Answers (1)

Kim Kern
Kim Kern

Reputation: 60357

You can inject HttpAdapterHost instead, see docs:

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

Then you can access the http adapter via this property:

const httpAdapter = this.adapterHost.httpAdapter;

The library instance you can get with:

const instance = httpAdapter.getInstance();

Upvotes: 9

Related Questions