简体   繁体   English

通过服务器端多个HTTP请求到第三方API的快速路由

[英]Express routing with server-side multiple HTTP requests to 3rd party API

I want to use a button in my AngularJS app to make a client-side HTTP request to NodeJS then from within NodeJS make multiple server-side HTTP requests to a 3rd party API. 我想使用AngularJS应用程序中的按钮向NodeJS发出客户端HTTP请求,然后从NodeJS内向第三方API发出多个服务器端HTTP请求。 I'm having trouble making the multiple HTTP requests to the 3rd party API. 我在向第三方API发出多个HTTP请求时遇到了麻烦。 I am using AngularJS 1.5. 我正在使用AngularJS 1.5。

myController.js myController.js

function onClick() {
  return MyAngularService.lookup()
    .then(function(response) {
      console.log(response)
    })
}

my-angular-service.js my-angular-service.js

function lookup()  {

  return $http({
    method: 'GET', 
    url: '/lookup’
  })
}

lookup-route.js lookup-route.js

const rp = require('request-promise')
const Promise = require('bluebird')
const router = require('express').Router()

router.get('/lookup', lookup) 


function lookup(req, res, next) {

    const urls = ['https://jsonplaceholder.typicode.com/todos/1', 'https://jsonplaceholder.typicode.com/todos/2']

    const doRequest = function(url) {
      return rp(url)
        .then(function(response) {
          return res.json(response)
        })
    }

    const requests = [];

    for (let i=0; i < urls.length; i++) {
      const url = urls[i]
      requests.push(doRequest(url))
    }
    return Promise.all(requests)
      .then(res.json(requests))
 }

When I console.log(response.data) in the myController.js I get {isFulfilled: false, isRejected: false} for each of the 3rd party urls I want to GET data from. 当我在myController.js中的console.log(response.data) ,对于myController.js中获取数据的每个第三方URL {isFulfilled: false, isRejected: false}我都会得到{isFulfilled: false, isRejected: false}

How can I return the JSON from the 3rd party API back to my controller? 如何将JSON从第3方API返回到我的控制器?

What jumps out to me in the server-side code is that: 在服务器端代码中跳出来的是:

  1. Sending res.json inside of each "doRequest" function (which means you will get back one response from the 3rd party API -- whichever fulfills first) 在每个“ doRequest”函数内部发送res.json (这意味着您将从第三方API取回一个响应-以先满足者为准)
  2. Chaining on the end of the Promise.all method by doing .then(res.json(requests)) which causes the callback to run early; 通过执行.then(res.json(requests))在Promise.all方法的末尾链接,这会导致回调提早运行; instead of doing something like .then(res.json) or .then(function(data){ return res.json(data); }) 而不是执行.then(res.json).then(res.json) .then(function(data){ return res.json(data); })

Try updating the implementation of the express route -- 尝试更新快递路线的实施方式-

const rp = require('request-promise')
const router = require('express').Router()

router.get('/lookup', lookup) 

function lookup(req, res) {

    const urls = ['https://jsonplaceholder.typicode.com/todos/1', 'https://jsonplaceholder.typicode.com/todos/2']

    // map the array of urls to an array of promises
    const promisesArray = urls.map(function(url){
        return rp(url);
    });

    // once all promises are fulfilled, return array of data as json response
    Promise.all(promisesArray).then(function(responseArray){
        return res.json(responseArray)
    });
 }

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

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