Reputation: 562
I am trying to use data that the user inputs to make an api call to an external API using node. I have successfully made calls from the server, and returned them to angular. But I need to send the server a variable from angular, have it make a call, and return the response to my view. I am new to node so I am sorry if this has already been answered somewhere. I looked around and found nothing.
My html
<md-input-container class="md-block" flex-gt-sm>
<label>Player 1</label>
<input ng-model="player1.username">
</md-input-container>
<md-button ng-click="comparePlayers(player1, player2)">COMPARE!</md-button>
My controller function
$scope.comparePlayers = function(player1) {
Nerd.getPlayer(player1)
}
My 'Nerd' Service
smiteStats.factory('Nerd', ['$http', function($http) {
return {
getPlayer: function(playerName) {
$http.post('/api/getPlayer', playerName).success(function(data) {
console.log("sent to server"); //does not run
})
}
}]);
My Express Route
app.post('/api/getPlayer', GetPlayer.apiGetPlayer);
My node module that makes the api call
module.exports = {
apiGetPlayer: function(error, res, player) {
console.log(player); //this returns "[Function: next_layer] in my cmd
}
}
Upvotes: 1
Views: 814
Reputation: 825
To send Parameters with $http in POST:
$http({
url: host + '/api/getPlayer',
method: "POST",
data: { 'fruit1' : "Apple"}
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
To get POST parameters, you will need the ExpressJS body-parser package. This will allow you to grab information from the POST.
Upvotes: 1