0

I have a made a NestJS microservice package and separate NestJS client application for contacting the microservice. Below given is the code used in the client application. In microservice the method used is @messagePattern and it is functional. My question is how a front-end app can contact directly to the microservice without going through the client also how to setup swagger in microservice or test it directly from a postman ?

import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
import { ClientProxy, ClientProxyFactory, Transport } from '@nestjs/microservices';
import { errorResponse, goodResponse } from 'src/helpers/response.helper';
import { AddContractDTO } from './contract.dto';

@Injectable()

export class ContractService {
    private client: ClientProxy; 

    constructor() {
        this.client = ClientProxyFactory.create({
          transport: Transport.TCP,
          options: {
            host: '127.0.0.1',
            port: 3011,
          },
        });
      }

    public async addContract(data: AddContractDTO) {
            const res = await this.client.send<any,any>('contract/addContract', data).toPromise();
            console.log(res);
            if(!res.success){
              throw new BadRequestException(res)
            }
            return goodResponse(res.data.data,'Contract created');

    }
}
Ujjual
  • 958
  • 2
  • 10
  • 36

1 Answers1

5

You cannot call the service directly. You need to create a controller (to bind to an endpoint) which then can call the service.

Examples can be found in the NestJS Documentation (https://docs.nestjs.com/microservices/basics).

Schutt
  • 1,056
  • 6
  • 17