CORS: GET Request from plugin

Hey,

I am trying to make a very simple get request from a plugin I'm developing. I have tried a couple of things. Attached is an excerpt of some of this code.

const request = require('request');

function getRandom() {
  return Math.random() * 10;
}

function getInfo() {
  let info;

  console.log("magic happens here")

  request('http://google.com', function (err, res, body) {
    info = body.toString();
  });

  return info;
}

export default function (server) {

  server.route({
    path: '/yoinfo',
    method: 'GET',
    handler(req, reply) {
      reply({ info: getInfo(), name: "jimbo", rand: getRandom() });
    }
  });
}

Both name and rand are returned in the payload, but info is not. Any idea how I can make a simple http get request within the plugin server directory?

Also, how to log to the kibana stdout?

Thanks

Ok, so it turns out that it was a CORS issue (I assume from the output from:

var http = require('http');

export default function (server) {

  const url = `${server.info.uri}/api/status`;

  var metrics;

  http.get(url, function(res){
    var body = '';

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        metrics = JSON.parse(body);
    });
  }).on('error', function(e){
        metrics = "Got an error: " + e;
  });

  server.route({
    path: '/metrics',
    method: 'GET',
    handler(req, reply) {
        reply({
            metrics: metrics,
            other: "jimbo"
        });
    }
  });
}

Output

{
metrics: "Got an error: Error: connect ECONNREFUSED 127.0.0.1:5601",
other: "jimbo"
}

If you change the kibana url to localhost:9200 it will work. How can I overcome this?

In the first example your call to getInfo() needs to wait for request() to call the callback before returning. The callback is called asynchronously, so you'll need to use a promise or another callback to get this working.

const request = require('request');

function getRandom() {
  return Math.random() * 10;
}

function getInfo(callback) {
  console.log("magic happens here")

  request('http://google.com', function (error, res, body) {
    if (error) {
      // always check for errors
      callback(error);

      // return to prevent the rest of this function from running
      return;
    }

    // pass the body to the callback without an error
    callback(null, body.toString());
  });
}

export default function (server) {

  server.route({
    path: '/yoinfo',
    method: 'GET',
    handler(req, reply) {
      // call getInfo and it will call getInfoCallback() when it is done 
      getInfo(function getInfoCallback(error, info) {
        if (error) {
          // always check for errors
          reply(error);
          return
        }

        reply({
          info: info,
          name: "jimbo",
          rand: getRandom()
        });
      });
    }
  });
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.