简体   繁体   English

如何执行从Node到API的获取请求

[英]How to do a get request from Node to an API

I am trying to a get request to an API. 我正在尝试获取对API的请求。

This is what I have in Angular 这就是我在Angular中拥有的

Service: 服务:

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

Ctrl: 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) {   
      console.log(Search);       
      Search.get({string: $scope.searchStr})
      .$promise.then(function(data) {
        console.log(data);
        $scope.responseData = data;
      })
    }

});

View: 视图:

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

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

Nodejs so far: 到目前为止的Node.js:

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

what I need is to get a response from that link above when the user is searching, but all I get is the response from the first get to api/show/:search , what am I missing? 我需要的是在用户搜索时从上面的链接获得响应,但是我所得到的只是对api/show/:search的第一个get的响应,我还缺少什么?

replace search with req.params.search 用req.params.search替换搜索

app.get('api/show/: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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM