Reacting
Reacting

Reputation: 6123

How to do a get request to a route and retrieve information from an API?

I am trying to do a get/query request from Angular using $resource to a specified route which will ping an API and then I should get an object with the results coming from the API.

This is a search function. See the flow of this issue:

Angular service:

angular.module('MyApp')
  .factory('Search', function($resource) {
    return $resource('/api/shows/:_search');
  });

Ctrl:

$scope.$watch('searchStr', function (tmpStr)
{
  if (!tmpStr || tmpStr.length == 0)
    return 0;


    // if searchStr is still the same..
    // go ahead and retrieve the data
    if (tmpStr === $scope.searchStr) {   
      Search.query({'search': $scope.searchStr})
      .$promise.then(function(data) {
        $scope.responseData = data;
      })
    }
});

View:

<input type="text" data-ng-model="searchStr">

<textarea> {{responseData}} </textarea>

Nodejs:

app.get('api/shows/:search', function(req, res, next) {
  console.log(req, res);
  request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
    console.log(error, response, body);
  });
});

there is what I need, I need to do a get request to 'api/shows/:search' and do something in order to get the results from http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search but I am still struggling how it should be done. The search param is the string coming from Angular in order to go to the thetvdb and return what I need.

Here is an example of what it should be return in case that you are sending the string param with the word "all": http://www.thetvdb.com/api/GetSeries.php?seriesname=all&language=en

Any suggestions?

Upvotes: 0

Views: 58

Answers (1)

chresse
chresse

Reputation: 5815

at least your nodejs route has to return the string in the response:

app.get('api/shows/:search', function(req, res, next) {
  request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
    console.log(error, response, body);
    res.end(body);
  });
});

Upvotes: 1

Related Questions