简体   繁体   English

如何从Node.js / Express中的URL获取JSON对象

[英]How to GET JSON Object from URL in Node.js/Express

I should preface my post by saying that I am a beginner and this is my first time using Node.js and Express in a real project. 我应该在帖子前面说我是初学者,这是我第一次在一个真实的项目中使用Node.js和Express。

I have a simple Node.js/Express project and I want to read a JSON object from a URL. 我有一个简单的Node.js / Express项目,我想从URL读取一个JSON对象。 Afterwards, I intend to build another url that displays html from an external website using iframe. 之后,我打算使用iframe构建另一个显示外部网站html的网址。

I read about the 'request' module online and know that I need to do something along these lines: 我在线阅读了“请求”模块,并知道我需要按照以下方式做一些事情:

var express = require('express');
var router = express.Router();
var request = require('request');

// Urls for App Center REST functions
var url = 'https://someserver.com/appserver/portal/api/1.0/results/recent';

/* GET list of recent reports */
router.get('/testapi', function(req, res, next) {
  res.render('testapi', { title: 'List Recent Reports' });
});

/* TEST: function to GET report list */
router.get('/recentreports', function(req, res){
  request({
    url: url,
    json: true
  }, function (error, response, body) {
      if (!error && response.statusCode === 200) {
        console.log(body) // Print the json response
    }
  })
});

I have tried to define a function /recentreports which is called in the testapi.jade view, however nothing is printed in the console when I load the page and I suspect I am doing something horribly wrong. 我试图定义一个在testapi.jade视图中调用的函数/recentreports testapi.jade ,但是当我加载页面时,控制台中没有打印任何内容,我怀疑我正在做一些可怕的错误。

My questions are: 我的问题是:

  1. How do I read the JSON into my app and where does this code go (index.js, app.js, testview.jade etc...?) 如何将JSON读入我的应用程序以及此代码的位置(index.js,app.js,testview.jade等...?)

  2. How do I export the URL I construct from wherever that code lives to my .jade view? 如何将我构建的URL从代码所在的任何地方导出到我的.jade视图?

There was nothing logged to the browser console because no response was sent from your server. 没有任何内容记录到浏览器控制台,因为没有从您的服务器发送响应。 The response was only logged to the server's console. 响应仅记录到服务器的控制台。

You'll need to refactor the code for the 'recentreports' route to send data. 您需要重构'recentreports'路由的代码才能发送数据。 You could use a simple res.send call: 您可以使用简单的res.send调用:

...
function (error, response, body) {
  if (!error && response.statusCode === 200) {
    res.send(body) // Send the response to the client
  }
}
...

This response will be received by testapi.jade via an AJAX call to the '/recentreports' route. testapi.jade通过对'/recentreports'路径的AJAX调用接收此响应。 The AJAX call can be defined in a Javascript file sourced by the testapi.jade file. 可以在testapi.jade文件源代码的Javascript文件中定义AJAX调用。

The constructed URL would not need to be exported as it exists within the same testapi.jade file (after you've formed it from the results from the AJAX call). 构造的URL不需要导出,因为它存在于同一个testapi.jade文件中(在您从AJAX调用的结果中形成它之后)。

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

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