Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe stream to reply in hapi.js

Im looking for the parllel method in hapi

// Express + Request exmaple
function(req, res){
  request('http://example.com/image.png').pipe(res);
}

How to pipe a response in hapi ?

server.route({
method:  "*",
path:    "/api/results/{date}",
handler: (request, reply) => {


    //????reply(?);



}
});  
like image 309
doron aviguy Avatar asked Jun 30 '15 06:06

doron aviguy


2 Answers

From another question/answer:

function (request, reply) {

    Request('http://example.com/image.png')
    .on('response', function (response) {
        reply(response);
     });
}

https://stackoverflow.com/a/31222563/2573244

like image 127
Clarkie Avatar answered Nov 17 '22 20:11

Clarkie


If you only need to forward an upstream response, you can just use a proxy handler via the h2o2 plugin:

server.route({
    method: 'GET',
    path: '/upstream/file',
    handler: {
        proxy: {
            uri: 'http://example.com/image.png'
        }
    }
});
like image 2
Gil Avatar answered Nov 17 '22 22:11

Gil