Home > other >  Why use NestJS createMicroservice over NestFactory.create?
Why use NestJS createMicroservice over NestFactory.create?

Time:10-11

in my team we use NestJS for our backend. We want to use microservices architecture and I've seen there is a built-in microservices support in NestJS. After read the documentation I've got a few questions about this feature:

  1. Should I create microservices in the same project or in separate projects?
  2. Why even use createMicroservice over NestFactory.create? Using createMicroservice also makes me to use @MessagePattern and use an emitter to send messages - which is okay if my microservice gets messages using queue (like RabbitMQ), but I can't create REST API in my microservice, so it makes it harder (or impossible) to call NestJS microservice from non-NestJS microservice since it relies on NestJS MessagePattern.

Thanks ahead.

CodePudding user response:

  1. It is not necessary but I personally create separate projects within a workspace.
  2. Calling createMicroservice is compulsory because nest needs to know what kind of controller you have and what behavior do you expect. In addition, you should avoid implementing rest API in all services; it should be implemented just in gateway service which is responsible for API layer, and it is a bond between microservices and outworld.

For having a good notion about microservice implementation with NestJS I highly recommend you to take a look to this repo: https://github.com/Denrox/nestjs-microservices-example

  • Related