Thursday 4 May 2017

NodeJs HTTP Post call, how to use it as an API for AngularJs which uses $http

I have a NodeJS file which calls POST and get data from Azure. I want to convert it into API which can be used by AngularJS $http.

var http = require('http');
var https = require('https');
var querystring = require('querystring');
function getPred() {
    var data = {
        "Inputs": {
    "input1":
    {
        "ColumnNames": ["Age", "Gender", "Chest Pain Type", "Resting BP", "Serum Cholestrol", "Fasting Blood Pressure", "ECG", "Max Heart Rate", "ST Depression"],
      "Values": [ [ "20", "1", "4", "130", "322", "0", "2", "109", "0" ] ]
    },        },
"GlobalParameters": {
}
};

var dataString = JSON.stringify(data);
var options = {
host: 'ussouthcentral.services.azureml.net',
port: 443,
path: '/workspaces/8671b8e183d84ca58badf842d26ccdc6/services/8cde3573ca814c89967265a80c84cbd9/execute?api-version=2.0',
method: 'POST',
headers: {'Content-Type':'application/json', 'Authorization':'Bearer APIKEY'}
};

var reqPost = https.request(options, function (res) {

    res.on('data', function(d) {
    console.log("Output")
    process.stdout.write(d);
    });
});
reqPost.write(dataString);
reqPost.end();
reqPost.on('error', function(e){
console.error(e);
});
}
http.createServer().listen(8050);
console.log("Server is now running on port 8050");
getPred();

In general, how can I convert this post into an API call where i can use to post it using AngularJS.

my routing would be:

app.post('/api/user/insertUser/',userCtrl.Signup);

In angularJS:

$http.post('/api/user/insertUser', signup_data).then(function(response){
    .....
});

I would pass the data in signup_data and in the response, I would get the data I need.

Is there any way where I can convert the above POST request so that I can use it through AngularJS.



via Varun

No comments:

Post a Comment