Transports
Transports are a type of handler that allows to connect one pipe to the other via some kind of transmission protocol/transport, like http, tcp, gRPC, etc.
In the below example we are using Hapi Wreak module to make an actual call to the external service
_httptransport.js
module.exports = function transport(pipe, config) {
pipe.on('request', request => {
Wreck.request(request.method,
config.url, request, (err, response) => {
if (err) return pipe.throw(err);
Wreck.read(response, (err, body) => {
if (err) return pipe.throw(err);
response.body = body;
pipe.respond(response);
});
});
});
}
Transport example using http protocol as a base
example.js
var Http = require('http');
function transport(pipe, config) {
pipe.on('request', function (request) {
var options = Object.create(config);
options.path += '?' + Querystring.stringify(request);
var req = Http.request(options, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
res.body = data;
pipe.respond(res);
});
});
req.on('error', function (err) {
pipe.throw(err);
});
req.end();
});
}
var pipe = Trooba.use(transportFactory, {
protocol: 'https:',
hostname: 'www.google.com',
path: '/search?q=nike'
}).build();
pipe.create().request({
q: 'nike'
}, (err, response) => console.log);
pipe.create().request({
q: 'nike'
})
.on('error', console.error)
.on('response', console.log);
EDIT Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.