How and why I built Production ready Hapi boilerplate application with dual servers inbuilt.

Published Sep 07, 2017
How and why I built Production ready Hapi boilerplate application with dual servers inbuilt.

About me

I specialise in the design and architecture of enterprise and mobile applications. I have more than 12+ years of experience in application design, development, and delivery in the IT sector.

The problem I wanted to solve

Most modern web applications need two front-ends for users — one that’s browser based (for desktops/laptops) and another which is based on mobile devices. Browser-based frontends are typically used for administrative purposes and have limited features compared to their mobile counterparts, which are generally designed for end-users.

The concept behind Hapiness was to provide developers with a way to satisfy both of these needs by having the same hapi.js server cater to both kinds of front-ends, while reducing the redundancies of controllers and routes at the same time.

What is Production ready Hapi boilerplate application with dual servers inbuilt.?

'Hapiness' boilerplate application serves as a great starting point for all the Hapi.js developers who were looking for a platform for their production servers. This app instantiates two servers on startup:

localhost:8000 - Server for web application
localhost:8001 - Server for mobile/Rest APIs

Both the servers have their independent authentication mechanisms, the web application server uses the cookie based authentication and the api server uses the JWT authentication for the rest api access. The deployment and application management is done via 'Gulp' and there are different configurations/tasks for starting the app in the development vs the production environment. This application assumes the availability of 'MongoDB' installation on the localhost where the server will run.

The app contains basic user management with login, logout, password reset, profile view. Forgot password implementation is done using 'nodemailer' (sends an email with reset password link with a unique token that expires in 1 hour).

For production, we have used 'PM2' as the process manager which spawns 4 servers for the application and takes care of the rest of application life cycle management. All the environment variables are managed using 'dotenv' node package for development, the same will need to be configured at the host server in the production environment.

If you are using 'Chrome' for unit testing the application, you can Hapi-ly use the 'node-inspector' chrome plugin pre-configured with this app for debugging purposes.

Tech stack

Hapi - Server side framework
Handlebar - HTML templating engine
Mongoose - Mongo database ORM
SASS - CSS preprocessor
Gulp - Javascript tasks automation
WebPack - Asset pipeline
Dotenv - Environment variable emulator
Good - Logger mechanism
JWT - Authentication mechanism for APIs
Config - Configuration Handler
PM2 - Production process manager for Node.js apps
Nodemailer - Module to send emails

The process of building Production ready Hapi boilerplate application with dual servers inbuilt.

Here’s how it works:

This app instantiates two servers on startup:

  1. localhost:8000 – Server for web application
  2. localhost:8001 – Server for mobile/Rest APIs

Authentication
Both of these servers have independent authentication measures in place: the web application server uses cookie-based authentication, while the API server uses JWT authentication for rest API access. The deployment and application management is done via ‘Gulp’ and there are different configurations/tasks for starting the app in the development versus the production environment. This application assumes you already have ‘MongoDB’ as an available installation on the localhost.

Deployment
For production, we have used ‘PM2’ as the process manager. PM2 spawns a cluster with 4 member servers and takes care of the rest of application lifecycle management. All the environment variables are managed using ‘the dotenv’ node package for development. You will need to configure this on the production environment’s host server.

Unit Testing
If you are using the Chrome browser for unit testing the application, you can hapi-ly use the ‘node-inspector’ Chrome plugin pre-configured with this app for debugging purposes. This boilerplate app also comes with a clean, attractive user interface for login and dashboard access. See screenshots of that below.

The boilerplate code utilises ‘Joi’ for performing the validations on the user supplied information.

Logging
The logging aspect for this framework is covered with ‘Good’ which is a hapi plugin for logging. The custom style sheets are designed in SASS for better code syntax, performance and reusability. The template engine used for view development is ‘handlebars’. The configurations are organised using ‘Glue’ along with ‘Confidence’ which makes it very easy to switch between different plugins for the various functionalities while keeping the hapi’s ‘configuration over convention’ theme intact.

The application is designed in such a way that all the relevant artefacts are organised in their separate folders. You will see app, assets and config being the topmost container directories along with the root directory.

Challenges I faced

I tried to develop the framework easily to be understood by every developer, so that they can grab the flow and start using it to enhance this boilerplate into their own ideas.

Key learnings

Learn the power of Hapi.js. To give you an idea of just why the developer community believes Hapi.js is so robust and dependable, it was able to successfully manage thousands upon thousands of shopping transactions, returns and more at Wal-Mart during Black Friday, arguably the busiest retail shopping day of the year in the U.S. It does this by freely using plug-ins without making any one plug-in mandatory. And although Hapi.js makes applications modular, it’s still very flexible.

Tips and advice

As you can see, Hapiness is designed to save your hours of coding time by giving you a streamlined, efficient and clean-coded interface and architecture to base your own development on. Try it out and be sure to let us know your feedback!

Final thoughts and next steps

Our #Hapi.JS #StarterKit to help you build a #Stack off of a Hapi.js/Node.js #Framework. In future we can enhance this framework based on requirements.

Discover and read more posts from Arpit Khandelwal
get started
Enjoy this post?

Leave a like and comment for Arpit