sirensolutions/h2o2

Name: h2o2

Owner: Siren

Description: Proxy handler for hapi.js

Created: 2015-12-23 11:27:39.0

Updated: 2016-04-29 09:42:24.0

Pushed: 2017-04-26 14:14:56.0

Homepage: null

Size: 126

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

h2o2

Proxy handler plugin for hapi.js.

NPM

Build Status

Lead Maintainer - Oscar A. Funes Martinez

Introduction

h2o2 is a hapi plugin that adds proxying functionality.

Manual loading

Starting on version 9, hapi does not load the h2o2 automatically. To add h2o2 to your server, you should register it normally.

t Hapi = require('hapi');
t server = new Hapi.Server();

er.register({
register: require('h2o2')
unction (err) {

if (err) {
    console.log('Failed to load h2o2');
}

server.start(function (err) {

    console.log('Server started at: ' + server.info.uri);
});

NOTE: h2o2 is included with and loaded by default in Hapi < 9.0.

Options

The proxy handler object has the following properties:

Usage

As one of the handlers for hapi, it is used through the route configuration object.

reply.proxy(options)

Proxies the request to an upstream endpoint where:

No return value.

The response flow control rules do not apply.

t handler = function (request, reply) {

return reply.proxy({ host: 'example.com', port: 80, protocol: 'http' });

Using the host, port, protocol options

Setting these options will send the request to certain route to a specific upstream service with the same path as the original request. Cannot be used with uri, mapUri.

er.route({
method: 'GET',
path: '/',
handler: {
    proxy: {
        host: '10.33.33.1',
        port: '443',
        protocol: 'https'
    }
}

Using the uri option

Setting this option will send the request to an absolute URI instead of the incoming host, port, protocol, path and query. Cannot be used with host, port, protocol, mapUri.

er.route({
method: 'GET',
path: '/',
handler: {
    proxy: {
        uri: 'https://some.upstream.service.com/that/has?what=you&want=todo'
    }
}

Custom uri template values

When using the uri option, there are optional default template values that can be injected from the incoming request:

er.route({
method: 'GET',
path: '/foo',
handler: {
    proxy: {
        uri: '{protocol}://{host}:{port}/go/to/{path}'
    }
}

Requests to http://127.0.0.1:8080/foo/ would be proxied to an upstream destination of http://127.0.0.1:8080/go/to/foo

Additionally, you can capture request.params values and inject them into the upstream uri value using a similar replacment strategy:

er.route({
method: 'GET',
path: '/foo/{bar}',
handler: {
    proxy: {
        uri: 'https://some.upstream.service.com/some/path/to/{bar}'
    }
}

Note The default variables of {protocol}, {host}, {port}, {path} take precedence - it's best to treat those as reserved when naming your own request.params.

Using the mapUri and onResponse options

Setting both options with custom functions will allow you to map the original request to an upstream service and to processing the response from the upstream service, before sending it to the client. Cannot be used together with host, port, protocol, or uri.

er.route({
method: 'GET',
path: '/',
handler: {
    proxy: {
        mapUri: function (request, callback) {

            console.log('doing some aditional stuff before redirecting');
            callback(null, 'https://some.upstream.service.com/');
        },
        onResponse: function (err, res, request, reply, settings, ttl, data) {

            console.log('receiving the response from the upstream.');
            Wreck.read(res, { json: true }, function (err, payload) {

                console.log('some payload manipulation if you want to.')
                reply(payload).headers = res.headers;
            });
        }
    }
}


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.