Tuesday, March 17, 2015

Hapijs for Proxy Routes and mapUri

One of the strengths of Hapijs is the proxy route. This involves making a request to your Hapijs server and having it make a request to another server. Actually a reverse proxy.

First will be a simple, hardcoded proxy. Next example uses the Url query string. Final example uses Url path.

Hello World Proxy

Sometimes a proxy to an absolute url is all that is needed. It is very simple:


var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 8882
});
server.route({
  method: 'GET',
  path: '/',
  handler: {
    proxy: {
      uri: 'https://www.google.com/search?q=Hello+World'
    }
  }
});

In your browser go to: http://localhost:8882 to see the Google search for "Hello World".

Proxy Using Url Query for Input

But what if you want to search on a query besides "Hello World"? Or need the flexibility for something else. Hapijs provides a mapUri method for proxy to map the request uri to the proxied uri.

This means that your code can take the url, or parts of it, from a request and proxy to a modified uri. In our case the request will have a query ('q=hello+world') that we want to use in the proxy request. Add this route to the above code:


server.route({
  method: 'GET',
  path: '/search',
  handler: {
    proxy: { //https://www.google.com/?gws_rd=ssl#safe=active&q=hello+world
      mapUri: function(request, callback){
        var url='https://www.google.com/search?q='+request.url.query.q;
        callback(null, url);
      }
    }
  }
});

The browse to http://localhost:8882/search?q=hello+world to see the same Google page as above. Now browse to http://localhost:8882/search?q=hello+galaxy to see a search for "Hello Glaxay".

Proxy Using Url Path for Input

What if you wanted the proxy to change based on the path instead of using the query part of the url? For example, http://http://localhost:8882/hello+universe. Add this code to the above:


server.route({
  method: 'GET',
  path: '/{query}',
  handler: {
    proxy: { //https://www.google.com/?gws_rd=ssl#safe=active&q=hello+world
      mapUri: function(request, callback){
        var url="https://www.google.com/search?q="+request.params.query;
        callback(null, url);
      }
    }
  }
});

Now browse to http://http://localhost:8882/hello+universe to see a search for "Hello Universe". Or for something more down to earth: http://http://localhost:8882/earth.



Uses for Proxy


There are many ways to use the proxy. One example is to avoid Cross Site Scripting (CORS) issue. Or when replacing a legacy system a Hapijs server could be stood up with every route being a proxy to start. Then replace one route at a time with new Hapijs code. And of course, using an API where some manipulations is needed.

Conclusion

This tutorial shows how simple it is to do proxies with Hapijs with three short examples. There are several more options not covered above. See the Hapijs documentation for more details.

Note: Hapijs 8.4 was used for the above code.


Other Tutorial

Getting Started With Hapijs and Testing

No comments: