Hybrid Application

HTTP and a Microservice in One Process

A hybrid app serves HTTP and listens to a message transport. Useful when a service has a REST API for the frontend and also consumes events.

apps/orders/src/main.ts

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { OrdersModule } from './orders.module';

async function bootstrap() {
  const app = await NestFactory.create(OrdersModule);

  app.connectMicroservice<MicroserviceOptions>({
    transport: Transport.KAFKA,
    options: { client: { brokers: ['kafka:9092'] }, consumer: { groupId: 'orders' } },
  });

  await app.startAllMicroservices();   // Kafka consumer
  await app.listen(3000);              // HTTP
}
bootstrap();

Note: By default, global pipes, guards and interceptors registered with app.useGlobal...() apply only to HTTP. Pass { inheritAppConfig: true } as the second argument of connectMicroservice to share them.