0

I'm getting started with Koa ecosystem. I know it's based in generator functions (a topic that is pretty new for me, for now), and I'm having issues with a very simple task: Call an external web service, fetch the response and send it to the client app (Angular in my case).

The code:

var koa = require('koa');
var app = koa();
var koarouter = require('koa-router');
var router = koarouter();

router.post('/devices', function *(next) {
    var reqdata = {};  
    reqdata.info="request to send" 
    var options = {
        url: url_getDevices,
        headers: reqdata
    };

    var response = yield request(options);
    var info = JSON.parse(response.body);

    console.log(info);

});

I suppose that I have to manage with yield and next but I'm doing different tests and having issues, and I'd to know best practices with Koa in this case.

Thanks!

AlexAcc
  • 601
  • 2
  • 10
  • 28

1 Answers1

0

Are you just trying to send info as a response from the POST request handler? You can just send it as the body of your response. In Koa, this.body is an alias for this.response.body:

this.body = info;

I would also recommend checking out the docs for Koa on their website.

Saad
  • 49,729
  • 21
  • 73
  • 112