Mortgage Calculator -Node.js

  • Updated

Mortgage Calculator - Advanced Sample using Node.js

This sample demonstrates one Rule Application calling another. The mortgage calculator has been modified to look up the APR based on the term. The lookup is performed using an Execute JavaScript Function action. The function uses JQuery to call to a service that is hosted in Node.js. The service returns the APR by leveraging another JavaScript Rule Application.

The following is sample code that when executed will start a Node.js server that executes rules on a specific port. For more details on this approach, a working sample is available on the InRule Support Site.

var http = require('http'); var url = require('url');

//Lets define a port we want to listen to const PORT=8080;

var inrule = require('./Rating.min.js')

//We need a function which handles requests and send response function handleRequest(request, response) {

response.setHeader("Access-Control-Allow-Origin", "*");

var parsedUrl = url.parse(request.url, true); var query = parsedUrl.query;

var rate = {};

rate.TermInYears = query.termInYears;

var session = inrule.createRuleSession(); session.createEntity('RateMgr', rate); session.applyRules(function(log){

response.end(JSON.stringify(rate));

});

}

//Create a server

var server = http.createServer(handleRequest);

//Lets start our server server.listen(PORT, function(){

//Callback triggered when server is successfully listening. Hurray! console.log("Server listening on: http://localhost:%s", PORT);

});

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.