Using Glue with Hapi JS

As you can tell, I blog whenever I work on a project and hit a wall, only to later (later = 2 days of frustration) find the solution. This time I worked with Glue.

Glue is a plugin that works with hapi to configure a server relatively easy, and by relatively I mean, it’ll take you having to crash your server to find out what works.

Glue works by exporting a single function named compose to your hapi server. Inside of compose are a three parameters that work to configure your server automatically; from server connections to server plugins.

Screen Shot 2016-05-24 at 8.33.55 PM
The entire documentation for Glue is a single Github page

File Structure

  • server.js
    • This will be our entry point for our app
  • server_settings.json
    • This will be where we will define our server configurations with Glue
  • package.json
    • Where all our dependencies will live

Server.js

In a barebones Hapi server, our server config would look like this:

Screen Shot 2016-05-25 at 10.21.12 AM

In my server.js file, I have configured the connection, the route, and started the server all in one file.

Server.js with Glue configuration

Now let’s look at the difference in our server.js file with Glue configuring our server settings.

Screen Shot 2016-05-25 at 10.29.16 AM

But wait! Where are all our port connections and stuff? I placed those in a json file and imported them using a require statement.

Screen Shot 2016-05-25 at 10.33.14 AM
server_config.json

The compose takes all the settings in server_config.json and uses them in a callback function to the Hapi server. This was the part that totally perplexed me because I wasn’t certain how Glue fit with Hapi. I’ll update the post when I figure out how to automatically fire up the Redis connection with Glue. Until then, check out the resources below.

Other Resources

  • Dave Stevens has a great blog post on using Glue
  • Glue API
  • A full example of server configuration with Glue

Happy Coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s