3

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.

I have a simple Node.js/Express project and I want to read a JSON object from a URL. Afterwards, I intend to build another url that displays html from an external website using iframe.

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.

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...?)

  2. How do I export the URL I construct from wherever that code lives to my .jade view?

1
  • What do you mean "Read the JSON into my app" and "How do I export the URL I construct from where"? Commented Jun 1, 2016 at 20:50

1 Answer 1

1

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. You could use a simple res.send call:

...
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. The AJAX call can be defined in a Javascript file sourced by the testapi.jade file.

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).

Sign up to request clarification or add additional context in comments.

1 Comment

I think I see now, so I would already have my JSON in the client side application and could process my business logic there. Makes sense. I will have to play around with AJAX - still new to this sort of thing. Any recommended links for this sort of thing in node/express?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.