Koa: How to use koa to download the network image and preview the page

Created on 12 Sep 2016  路  4Comments  路  Source: koajs/koa

I find a lot of information is not found, I want to use Java as a background service, the use of node+angularjs, but do not know how to use koa to get Java background provides the test images, and displayed in the angularjs side, can you help me, thank you very much.

question

Most helpful comment

  1. http request client: get image from backend of Java (Java api: http://127.0.0.1:7001)
  2. image pipe to res (ctx.body = image) (Koa api: http://127.0.0.1:3000)
  3. browser http request client: get the image from Koa api.

Need more examples see https://github.com/koajs/examples.

All 4 comments

  1. http request client: get image from backend of Java (Java api: http://127.0.0.1:7001)
  2. image pipe to res (ctx.body = image) (Koa api: http://127.0.0.1:3000)
  3. browser http request client: get the image from Koa api.

Need more examples see https://github.com/koajs/examples.

Java background has been written, the front desk has been written, is poor koa this step does not know how to write, I use the koa-router and request module to write, but the picture does not come
.the following is my part of the code

get('/captcha', function *(next){
        var $self = this;
        yield (loginSer().captcha()
            .then(function (response) {
                $self.response.length = response.length;
                $self.response.type="image/png";
                $self.response.body = response;
}));

The result is the following
image

If I'm not mistaken I believe you should be able to yield the promise directly and rewrite that code like this:

get('/captcha', function *(next){
    const image = yield loginSer().captcha()
    this.type = 'image/png'
    this.body = image
}))

It depends on what response if though, but I'm assuming it's a Buffer of png data.

^ @slaskis's got it

@dounine closing :) please let us know if you have any other questions

Was this page helpful?
0 / 5 - 0 ratings

Related issues

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

wlingke picture wlingke  路  3Comments

xinshouke picture xinshouke  路  4Comments

ke1Del picture ke1Del  路  3Comments

ElegantScripting picture ElegantScripting  路  5Comments