Skip to main content

Microservices

Nest Microservice

Bootstrap

Example 1:

import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { MicroserviceModule } from './microservice.module';

async function bootstrap() {
const app = await NestFactory.createMicroservice(MicroserviceModule, {
transport: Transport.TCP,
host: 'localhost',
port: 4200
});
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();

Example: 2

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/microservices';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const microservice = app.connectMicroservice({
transport: Transport.TCP,
options: {
host: 'localhost',
port: 3001,
},
});

await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();