Javascript 调用外部API并使用Node.JS显示JSON数据

Javascript 调用外部API并使用Node.JS显示JSON数据,javascript,json,node.js,express,Javascript,Json,Node.js,Express,我正在尝试使用 我想要实现的是在我的个人页面上显示我在Challonge上创建的所有锦标赛 我使用了request,可以从我的帐户中获取JSON格式的锦标赛数据,但是我不知道如何在index.html中显示它 var express = require('express'); var request = require('request'); var path = require("path"); var app = express(); app.get('/', function(re

我正在尝试使用

我想要实现的是在我的个人页面上显示我在Challonge上创建的所有锦标赛

我使用了request,可以从我的帐户中获取JSON格式的锦标赛数据,但是我不知道如何在index.html中显示它

var express = require('express');
var request = require('request');
var path    = require("path");
var app = express();

app.get('/', function(req, res, next) {
  request({
    uri: 'https://api.challonge.com/v1/tournaments.json',
    qs: {
      api_key: '<key_here>'
    }
  }).pipe(res);
});

app.listen(3000, function(){
    console.log('Server listening on port 3000');
});
如何在Node.JS index.html页面中显示我在Challonge上创建的所有锦标赛

My index.pug模板:

doctype html
html(lang='en')
  head
    title Tournaments
  body
    h1 #{tournaments}

谢谢,祝你今天愉快

您的对象形状不好。以下是is应该是[{tournament1},{tournament2}的样子,而不是像您的例子中那样进行另一个嵌套。使用此形状的对象,您可以使用:tournaments.ForEachFunctionTourban{/*使用此Tourban*/}轻松地对集合进行迭代,该集合是一个数组。然后,您可以使用一个名为jade的模板引擎(如pug was)并将tournaments对象传递给它,然后在名为index的视图中循环。阅读有关如何在express中设置模板引擎的文档

app.get'/',functionreq,res,next{ var tournaments=请求{ uri:'https://api.challonge.com/v1/tournaments.json', qs:{ api_密钥: } } res.render'index',{tournaments}; 下一个
};您可以使用pug来呈现数据。如何在应用程序中使用pug?只需运行npm install pug并查看express中有关如何连接模板引擎的文档。该对象形状是从Challonge API检索的。如何在您创建的tournaments变量中显示tourban.name?我已用我的p更新了我的第一篇文章ug模板,但是我只看到[object object],而不是锦标赛名称。我还尝试将h1{tournaments}修改为h1{tournaments.tournament}或h1{tournaments.tournament.name}但它不起作用。如果它不是您的api,请确定。首先通过执行:var tournaments=request/*api调用*/选择所有锦标赛对象。然后通过执行:然后像这样迭代对象:tournaments.forEachfunctiontour{tour.tourban.name};使用forEach时,抛出以下错误:TypeError:tournaments.forEach不是函数您的数据是一个数组,它应该正常工作。请尝试tournaments.map。它作为forEach进行迭代。
doctype html
html(lang='en')
  head
    title Tournaments
  body
    h1 #{tournaments}