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 app
instance was missing an initialization:
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();
It seems that app.listen
calls app.init
internally , now that we don't rely on app.listen
we have to call app.init
directly before getting the express
middleware.