Rules Engine in node.js

Edison Devadoss
YavarTechWorks
Published in
2 min readMar 11, 2020

In this article, I will explain the rules engine and how to implement it in our node.js application.

Rule Engine

What is the Rule Engine?

A business rules engine is a software system that executes one or more business rules in a runtime production environment. — wiki

Using the rules engine, you no need to change the business logic of your code as and when the changes required. User setup rules from the Frontend. Based on the rules, the application works.

When should we use the Rule Engine?

When we feel our traditional approach is not helpful at the time we can Rule Engine. Below I have given rules

  • The logic changes often
  • The problem is too complex to solve using the traditional method.

Implement with Node.js application

Project initial setup and install plugins

$ npm install fastify json-rules-engine

json-rules-engine is a powerful, lightweight rules engine. Rules are composed of simple JSON structures, making them human-readable and easy to persist.

Package.json

https://carbon.now.sh/

Setup index.js code

https://carbon.now.sh/

Write the code for the rule engine

https://carbon.now.sh/

In the above code, we add a condition for temperature that equal to 100. And pass facts as a parameter and check whether it matches or not. If the condition matches successfully it calls inside the onSuccess() block. If the condition matches failed it call inside the onFailure() block.

--

--

Edison Devadoss
YavarTechWorks

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader