CarlosMagalhaes
CarlosMagalhaes

Reputation: 91

How can I develop an API in nestJS that uses simultaneous Express and RabbitMQ?

const app = await NestFactory.create<NestExpressApplication>(AppModule, {
 });
 
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.RMQ,
options: {
urls: "amqps://user:pass@localhost",
queue: "queue",
queueOptions: {
durable: true,
 },
 },
 });

My api needs to received a JSON from httpRequest and then send a push to RabbitMQ

Upvotes: 0

Views: 349

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70442

If all you're needing to to make calls to RabbitMQ, then you can use the ClientsModule and create a microservice client as a part of your REST server, as described in the docs here. You can add this to your imports:

ClientsModule.register([
  {
    name: 'RABBIT_SERVICE',
    transport: Transport.RabbitMQ,
    options: {
      urls: "amqps://user:pass@localhost",
      queue: "queue",
      queueOptions: {
        durable: true,
      },
    },
  },
])

And now you Can use @Inject('RABBIT_SERVICE') to inject the RabbitMQ Client in your provider

Upvotes: 1

Related Questions