'How to execute the Kafka Consumer file from Javascript

I have used KafkaJS library and configured an consumer code to fetch messages from a topic of one of my streaming API.

I have created a consumer.js file and I'm able to run the file from the location in the commandline using node consumer.js

But my requirement is to execute the consumer.js file from my javascript/cypress code. I have put the complete consumer.js code inside a method under a Class and tried to call but it fails.

Is there a way to run the node consumer.js command directly from the code OR else how can I call it from my class.

Tried the consumer code similar to this https://thecodebarbarian.com/getting-started-with-apache-kafka-in-node-js.html

Any help would be highly appreciated.

Thanks



Solution 1:[1]

cy.exec() allows for execution of system commands. Note: there are some weird things to consider with this, that aren't readily apparent and could cause unexplained behaviors.

cy.exec('node consumer.js')
  // may be worthwhile to assert that the file ran successfully
  .its('code')
  .should('equal', 0);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 agoff