Add Routes and Handlers to Hapi

Reading time ~2 minutes

In the first post of the HapiJS series we learned how to use the server interface to build a server. Today let’s make an API we can use by adding routes and request handlers. Create a new file like so:

File: server.js
var Hapi = require('hapi');

var server = new Hapi.Server();

server.connection({ port: 9000 });

server.start(function () {
    console.log('API up and running at %s', server.info.uri);
});

Because HapiJS is designed for configuration over code, adding routes is just a matter of passing a JavaScript object to the server#route function.

File: server.js
server.route({
    path: '/lotto-numbers/{unixDate}',
    method: 'GET',
    handler: function (request, reply) {
        console.log('headers as obj', request.headers);
        console.log('query params as obj', request.query);
        console.log('client IP is %s', request.info.remoteAddress);
        console.log('path param unixDate is %s', request.params.unixDate);
    
        reply({ numbers: '1 12 36 22 50 2' });
    }
});

You can follow how easy it is to add a route:

  1. Providg a URI for the path
  2. Specify the HTTP method to respond to
  3. Write a handler function for the incoming HTTP request

The first parameter in the handler function is Hapi’s request object. It encapsulates the data you would expect to have access to like headers, query parameters, and path parameters but includes other goodies that are part of the Hapi ecosystem too. I suggest looking at the documentation to see what else is available to you.

File: server.js
server.route({
    path: '/bankaccount/{id}/deposit',
    method: 'POST',
    handler: function (request, reply) {
        // A JSON request body might look like { "amount": 100000000 }
        var amount = request.payload,
            accountId = request.params.id;
        
        var deposited = BankAccount.deposit(accountId, amount);
        if (deposited) {
            reply({ status: 'deposit complete').status(201);
        }
        else {
            reply({ status: 'deposit incomplete' }).status(500);
        }
    }
});

Expanding sliders with Kendo UI.

I've starting hitting walls with Kendo UI not supporting some expected functionality. The Angular UISlider makes it easy change the confi...… Continue reading

Up and Running with ReactJS

Published on June 10, 2015

Freelancing Inspiration

Published on June 09, 2015