https://docs.nestjs.com/microservices/basics
import {
Body,
Controller,
Get,
HttpCode,
Logger,
Param,
Post,
} from "@nestjs/common";
import { Client, ClientProxy, Transport } from "@nestjs/microservices";
import { ApiTags } from "@nestjs/swagger";
import { BodyMathDto } from "./dto/body-math.dto";
@ApiTags("math")
@Controller("math")
export class MathController {
private readonly logger = new Logger(MathController.name);
@Client({
transport: Transport.TCP,
options: {
host: "127.0.0.1",
port: 8877,
},
})
private readonly client: ClientProxy;
@Get()
getHello() {
return this.client.send<string, string>("getHello", "");
}
@Post("add")
@HttpCode(200)
add(@Body() body: BodyMathDto) {
return this.client.send<string, BodyMathDto>("add", body);
}
@Post("subtract")
@HttpCode(200)
subtract(@Body() body: BodyMathDto) {
return this.client.send<string, BodyMathDto>("subtract", body);
}
@Post("multiply")
@HttpCode(200)
multiply(@Body() body: BodyMathDto) {
return this.client.send<string, BodyMathDto>("multiply", body);
}
@Post("divide")
@HttpCode(200)
divide(@Body() body: BodyMathDto) {
return this.client.send<string, BodyMathDto>("divide", body);
}
@Get(":id")
testParam(@Param("id") id: string) {
return this.client.send<string, string>("testParam", id);
}
}