简体   繁体   English

我可以在一条路由中多次使用npm request并将结果显示在同一页面上吗?

[英]Can I use npm request multiple times in one route and have the results display on the same page?

Is there a way to use node module "request" multiple times in one route and have the results display on the same rendered ejs template page? 有没有一种方法可以在一条路由中多次使用节点模块“请求”,并使结果显示在同一渲染的ejs模板页面上?

Goal: I want to display eBook information from the iTunes Store using their Search API. 目标:我想使用他们的Search API显示来自iTunes Store的电子书信息。 Right now I can get an ebook's price in one territory, but I would like to display prices for the same ebook in multiple countries. 现在,我可以在一个地区获得电子书的价格,但我想显示多个国家/地区的同一本电子书的价格。 To get each different price, I need to use a different URL, either a default ending, "&country=gb", "&country=fr" etc. I can get prices for each of the 5 countries I am interested in, but I can't get them all on the same page. 要获得每个不同的价格,我需要使用不同的URL,或者使用默认的结尾“&country = gb”,“&country = fr”等。我可以获取我感兴趣的5个国家/地区中每个国家的价格,但是我可以不能将它们全部放在同一页面上。 How can I construct my route and prepare my variables so that I can display information from all of these countries when I render one page? 如何绘制路线并准备变量,以便在呈现一页时可以显示所有这些国家/地区的信息?

I am using node, express and ejs, and I am making the http request using the "request" node module. 我正在使用node,express和ejs,并且正在使用“请求”节点模块发出http请求。 Here are the basics of the form I'm using, where I enter the ISBN in the searchTerm field. 这是我正在使用的表单的基础,我在searchTerm字段中输入了ISBN。 (there's more, but it's bulky.) (还有更多,但体积很大。)

<form action="/search">
  <select name="searchTerm">

Code that is working now for getting info on the book in one territory: 现在正在使用的代码可在一个区域中获取有关该书的信息:

app.get('/search', function(req, res){
  var searchRequest = req.query.searchTerm;
  var searchURL = "http://itunes.apple.com/lookup?isbn=" + searchRequest;
  request(searchURL, function(error, response, body){
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      console.log(data.results[0]);
      var iTunesResults = data.results[0];
      res.render("test", {
        iTunesResults: iTunesResults,
        searchRequest: searchRequest
      });
    }
  });
});

If I make a few changes in this code I can get information on the same ebook in a different country with just a few changes. 如果我对此代码进行了一些更改,仅需进行一些更改,就可以获取不同国家/地区的同一本电子书的信息。 Below is the code for great britain, and it works on its own, I just want to get results from both of these searches displaying at once, and I'm not sure where to begin. 以下是英国的代码,它可以独立工作,我只想同时显示这两个搜索的结果,而且不确定从哪里开始。

app.get('/search', function(req, res){
  var searchRequest = req.query.searchTerm;
  var gbSearchURL = "http://itunes.apple.com/lookup?isbn=" + searchRequest + "&country=gb";
  request(gbSearchURL, function(error, response, body){
  if (!error && response.statusCode == 200) {
    var data = JSON.parse(body);
    console.log(data.results[0]);
    var iTunesResults = data.results[0];
    res.render("test", {
      iTunesResults: iTunesResults,
      searchRequest: searchRequest
    });
    }
  });
});

What I want to do is make an additional request to Apple, using the same search term here but adding a country identifier to the end of the searchURL I'm passing into request. 我想要做的是向苹果发出额外的请求,在这里使用相同的搜索词,但在我传递给请求的searchURL的末尾添加国家/地区标识符。 However, if I try to call request again and pass all of the results variables into res.render at once, the result of the first request will not be available inside the second request.("ReferenceError: iTunesResults is not defined") 但是,如果我尝试再次调用请求并将所有结果变量一次传递到res.render中,则第一个请求的结果将在第二个请求中不可用。(“ ReferenceError:未定义iTunesResults”)

I'm guessing there is a way to have an array of searchURL's and an array of results (req.query.searchTerm), then I can pass the array or a more complicated object in when I render the test.ejs template. 我猜想有一种方法可以拥有一个searchURL数组和一个结果数组(req.query.searchTerm),然后在渲染test.ejs模板时可以传递该数组或更复杂的对象。 Am limited to one http request per app.get route? 每个app.get路由限于一个http请求吗? (Forgive me if that should be obvious, but I just want to grab this pricing information and display it next to each other. Also, I figured it would be better to be verbose in my question.) (请原谅我,如果这应该很明显,但我只想获取此定价信息,并将其彼此相邻显示。另外,我认为在我的问题中进行详细说明会更好。)

Ok! 好! I got it resolved using the "async" node library. 我使用“异步”节点库解决了该问题。 http://www.github.com/caolan/async http://www.github.com/caolan/async

With that I can call multiple functions in parallel, and when they're done I can just pass back the results to a template to render. 这样,我可以并行调用多个函数,完成后,我可以将结果传递回模板进行渲染。

app.get('/search', function(req, res){
  async.parallel([
    function(done){
    var searchRequest = req.query.searchTerm;
    var gbSearchURL = "http://itunes.apple.com/lookup?isbn=" + searchRequest + "&country=us";
    request(gbSearchURL, function(error, response, body){
      if (!error && response.statusCode == 200) {
        // console.log(body) 
        var data = JSON.parse(body);
        // below shows whole object of result 0
        console.log(data.results[0]);
        done(null, data.results[0]);
      }
    });
  },
  // UK pricing
    function(done){
      var searchRequest = req.query.searchTerm;
      var gbSearchURL = "http://itunes.apple.com/lookup?isbn=" + searchRequest + "&country=gb";
      request(gbSearchURL, function(error, response, body){
        if (!error && response.statusCode == 200) {
          // console.log(body) 
          var data = JSON.parse(body);
          // below shows whole object of result 0
          console.log(data.results[0]);
          done(null, data.results[0]);
        }
      });
    },function(err, iTunesResults){
      res.render("test", {
        iTunesResults: iTunesResults,
        searchRequest: req.query.searchTerm
      });
  })
});

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

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