最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to inject HTTP server in Nest 6? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Apr 2, 2019 at 21:41 Kim Kern 60.6k20 gold badges217 silver badges214 bronze badges asked Apr 2, 2019 at 21:18 Niklas HigiNiklas Higi 2,3091 gold badge16 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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();
发布评论

评论列表(0)

  1. 暂无评论