【问题标题】:How to modify third party Api data using Nodejs 'Request' module, before sending it to frontend?在将第三方 Api 数据发送到前端之前,如何使用 Nodejs 的“请求”模块修改第三方 Api 数据?
【发布时间】:2017-05-09 18:39:24
【问题描述】:

我是 NodeJs 的新手,我尝试了谷歌搜索,但一无所获。我正在尝试从 API 链接修改 json 数据,但它正在作为原始 json 发送到前端 angularjs。

 app.get('/getCustomers', bodyParser.urlencoded({extended: false}), function(req, res){  
     var url = 'http://www.w3schools.com/angular/customers.php'
      var options = {
        method: 'GET',
        json: true,
        url: url
      }
      request(options, function (err, res, body) {
        if (err) {
          inspect(err, 'error posting json');
          return
        }
        var headers = res.headers;  //working
        var statusCode = res.statusCode; 
        console.log(statusCode); //200
        console.log(headers);
        console.log(res.body);//prints original data.
        res.body.extra='jack is cool';

      }).pipe(res);

Angularjs 代码:

$http.get('http://localhost:8080/yo')
    .then(function (response) 
    {
        $scope.getApiResponse = response.data;
        console.log($scope.getApiResponse); //no modifications

    });

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: javascript angularjs json node.js


    【解决方案1】:

    检查APIInterceptors,它将在您的服务器和前端之间为您的请求和响应充当防火墙或网关 检查这个http://onehungrymind.com/winning-http-interceptors-angularjs/

    【讨论】:

    • 感谢回答,不过我想修改nodejs上的api数据。
    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2019-07-14
    • 2017-09-20
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多