使用 DaprServer 从 NestJs Web 应用程序公开端点

问题描述 投票:0回答:1

DaprServer
允许公开 express
 中间件实例中定义的 http api 端点。

在 NestJs 应用程序中,可以

获取应用程序的内部 express

 中间件实例
并将其传递给 DaprServer 实例:

import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { DaprServer } from '@dapr/dapr'; import type { Express } from 'express'; async function bootstrap() { const app = await NestFactory.create(AppModule); const middleware: Express = app.getHttpAdapter().getInstance(); const daprServer = new DaprServer({ serverHost: '127.0.0.1', serverPort: '3000', serverHttp: middleware, }); await daprServer.start(); // NestJs app.listen call is removed // await app.listen(3000); } bootstrap();
DaprServer 实例已启动,并且删除了对 

app.listen

 的调用,
但是控制器中定义的端点不可用。

是否可以通过这种方式将DaprServer与NestJs一起使用?

express nestjs dapr
1个回答
0
投票
NestJs

app

 实例缺少初始化:

import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { DaprServer } from '@dapr/dapr'; import type { Express } from 'express'; async function bootstrap() { const app = await NestFactory.create(AppModule); // Initialize the Nest app instance const initedApp = await app.init(); const middleware: Express = initedApp.getHttpAdapter().getInstance(); const daprServer = new DaprServer({ serverHost: '127.0.0.1', serverPort: '3000', serverHttp: middleware, }); await daprServer.start(); // NestJs app.listen call is removed // await app.listen(3000); } bootstrap();
看起来

app.listen

在内部调用了
app.init
,既然我们不依赖
app.listen
,我们必须在获取
app.init
中间件之前直接调用
express

© www.soinside.com 2019 - 2024. All rights reserved.