DaprServer allows to expose http api endpoints defined in an express middleware instance.
In a NestJs app, it is possible to get the application's internal express middleware instance and pass it to the DaprServer instance:
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();
The DaprServer instance is started and the call to app.listen is removed, however endpoints defined in controllers are not available.
Is it possible to use DaprServer with NestJs in this way ?
The NestJs
appinstance was missing an initialization:It seems that
app.listencallsapp.initinternally , now that we don't rely onapp.listenwe have to callapp.initdirectly before getting theexpressmiddleware.