Search code examples
node.jscorsibm-mobilefirstibm-cloud

How to do a cors request to a node app in Bluemix


i'm making a nodeapp on bluemix that is bounded to the business rules service and i have another application hosted on mobilefirst where i created my application UI. and i want to send data from the mobilefirst application and receive data, i've been told to use a cors request but i don't know how to do this.

here is my app.js file :

var app = require("express")(),
restler = require("restler"),
bodyParser = require("body-parser");

app.use(bodyParser.json());

app.use(function(request, response, next) {
    response.header("Access-Control-Allow-Origin", "*");
    response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,Content-Type, Accept");
    next();
});


app.post("/", function(request, response) {
    var options = {
        username: "resAdmin",
        password: "replace"
    };

    var url = "https://brsv2-6855bc66.ng.bluemix.net/DecisionService/rest" +  "/vacationsRuleApp/1.0/vacationsRuleProject/json";

    restler.postJson(url, request.body, options).on('complete', function(data) {
        response.send(data);
    });
});

app.listen(process.env.VCAP_APP_PORT || 8080);

and here is my client code :

$.post( 
    "http://businessrules-vacationsruleapp.mybluemix.net/",
    {
        "employeeID": "jujuju",
        "loanAmount": 10517320,
        "theEmployee": {
        "seniority": 3,
        "annualSalary": 10517320,
        "nbOfExtraVacationDaysBasedOnSeniority": 10517320
    },
    "creditAmount": 20000,
    "__DecisionID__": "string",
    "AnnualSalary": 20000 },
    function( data ) {
        alert(JSON.stringify(data));
        console.log(data);
    }
);

Solution

  • You would need to include the following in app.js. The following will allow CORS on requests.

    app.js

    var app = require("express")(),
     restler = require("restler"),
     bodyParser = require("body-parser");
    
    app.use(bodyParser.json());
    
    app.use(function(request, response, next) {
        response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });
    
    
    app.post("/", function(request, response) {
        var options = {
            username: "replace",
            password: "replace"
        };
    
        var url = "https://brsv2-6855bc66.ng.bluemix.net/DecisionService/rest" + "/vacationsRuleApp/1.0/vacationsRuleProject/json";
    
        restler.postJson(url, request.body, options).on('complete', function(data) {
            response.send(data);
        });
    });
    
    app.listen(process.env.VCAP_APP_PORT || 8080);
    

    Then client side with JQuery you could do the following.

    $.ajax ({
        url: "https://businessrules-vacationsruleapp.mybluemix.net",
        type: "POST",
        data: JSON.stringify({ 
        "employeeID": "jujuju",
        "loanAmount": 10517320,
        "theEmployee": {
            "seniority": 3,
            "annualSalary": 10517320,
            "nbOfExtraVacationDaysBasedOnSeniority": 10517320
        },
        "creditAmount": 20000,
        "__DecisionID__": "string",
        "AnnualSalary": 20000 }),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data){
            console.log(data);
            $(".result").text(JSON.stringify(data));
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="result"></div>

    Whatever object you send from the post call will get sent to the Business Rules Service.