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.
- This will be our entry point for our app
- This will be where we will define our server configurations with Glue
- Where all our dependencies will live
In a barebones Hapi server, our server config would look like this:
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.
But wait! Where are all our port connections and stuff? I placed those in a json file and imported them using a require statement.
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.
- Dave Stevens has a great blog post on using Glue
- Glue API
- A full example of server configuration with Glue