Im looking for the parllel method in hapi
// Express + Request exmaple
function(req, res){
request('.png').pipe(res);
}
How to pipe a response in hapi ?
server.route({
method: "*",
path: "/api/results/{date}",
handler: (request, reply) => {
//????reply(?);
}
});
Im looking for the parllel method in hapi
// Express + Request exmaple
function(req, res){
request('http://example./image.png').pipe(res);
}
How to pipe a response in hapi ?
server.route({
method: "*",
path: "/api/results/{date}",
handler: (request, reply) => {
//????reply(?);
}
});
Share
edited Mar 30, 2016 at 12:44
Clarkie
7,5509 gold badges40 silver badges53 bronze badges
asked Jun 30, 2015 at 6:10
doron aviguydoron aviguy
2,9443 gold badges23 silver badges19 bronze badges
5
-
I'm not sure if you can. Does request support the streams2 api? From the hapi docs:
Stream object (Note - any Stream object must be patible with the "streams2" API and not be in objectMode)
hapijs./api#reply-interface – Clarkie Commented Jun 30, 2015 at 8:39 -
1
I've done it with the aws-sdk module:
reply(s3.getObject(params).createReadStream());
– Clarkie Commented Jun 30, 2015 at 8:40 - server.route({ method: "*", path: "/api/results/{date}", handler: (req, reply) => { var fs = require('fs'); var fileStream = fs.createWriteStream('./doron'); request('google./doodle.png').pipe(fileStream); reply(fileStream); } }); this throwes the follwoing exception Debug: internal, implementation, error Error: Stream must have a streams2 readable interface – doron aviguy Commented Jun 30, 2015 at 11:12
- That's what I saw which is why I said that I don't think the request module supports streams2. You could try npmjs./package/wreck but I'm not sure if that'll work either. – Clarkie Commented Jul 1, 2015 at 8:07
- 1 ok i'll look into wreck , my problem now is that request module doesnt support streams2 . My opinion , is that hapi should support , and encourage people to use streams. after all my investigation i found there is not enough docs , about streams support in hapi . – doron aviguy Commented Jul 5, 2015 at 7:31
2 Answers
Reset to default 7From another question/answer:
function (request, reply) {
Request('http://example./image.png')
.on('response', function (response) {
reply(response);
});
}
https://stackoverflow./a/31222563/2573244
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./image.png'
}
}
});