1

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
    }
}

1 Answer 1

1

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.

Read more

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, I can now console log it from my route.js on the server, but how can I pass req.body into the node module?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.