1

I am trying to retrieve data from a REST API in the server side (.js) and display it in my view (.jade)

I was able to get the data but was not able to send it to the view . This is how my code looks like :

 var BugData ='initial data' ;
 var https = require('https');

var optionsget = {
    rejectUnauthorized: false,
    host : 'My host', // here only the domain name
    // (no http/https !)
    port : 443,
    path : 'Mypath', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = https.request(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);

       res.on('data', function(d) {
        console.info('GET result:\n');

        BugData =d; 
        console.log('Show Data  : ***** \n' +d);  


    }); 

}); 

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

res.render('index', { ab:BugData});

BugData (was defined before )is the variable i am trying to send to the view but for some reasons it is empty and does not contain the variable 'd'

Does anyone know why or how can i solve this ? Thanks

SIB
  • 13
  • 6
  • are you sure it is empty, or is it a Buffer object ? If it is a buffer object, then you can fix in multiple ways. (e.g. `res.setEncoding("utf8");`) – mugabits Apr 27 '16 at 15:28
  • Your code never examines the value of BugData and it never does anything that looks like "sending to a view" – Quentin Apr 27 '16 at 15:31
  • please give whole code, where does Your code is located? inside of route? – num8er Apr 27 '16 at 15:32
  • 1
    I expect that when the missing code is added to the question that this will turn out to be *yet another* [duplicate of this](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Quentin Apr 27 '16 at 15:32
  • @num8er yes it is inside the router , i updated the post with the whole code – SIB Apr 27 '16 at 16:06

1 Answers1

1

There is no need to write that long code.

Be simple, follow these steps:

1) install request package:

npm install --save request

2) outside of router add:

var request = require('request');

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;

3) use this code inside router:

request.get({url: 'https://my-host/Mypath'}, function(err, response, body) {
      var data = {};

      if (err) {
          console.error(err);
          data.err = err;
      }

      data.ab = body;
      console.log('Data: ', data);

      res.render('index', data);
});
num8er
  • 18,604
  • 3
  • 43
  • 57
  • maybe they would prefer an answer without using additional modules? – lauriys Apr 27 '16 at 16:25
  • actually request module is: a. simple, b. universal, c. widely used by everyone. https module is good thing to be used as connection listener, than request maker. if You talk about additional module: https is also module. is there any difference if You user https.request(), or request.get() ? both of these are functions, nothing more, nothing special, it does not harm performance. – num8er Apr 27 '16 at 16:27