DEV Community

Sungmin
Sungmin

Posted on

Learning Hapi.js (1) - How I feel when I learned Hapi.js for the first time

Learning Hapi.js (1) - How I feel when I learned Hapi.js for the first time

I've ever heard of a couple of Node.js Framework like Express.js and Koa.js, but I've never heard of Hapi.js before I start working for the current company. However, I feel like what Hapi.js looks like(I mean syntax) is similar to the syntax of Express.js and, just for my personal opinion, Hapi.js looks easier to read for me than Express.js. Because of several reasons, I would like to dive into Hapi.js.

What is Hapi.js?

Hapi.js is an open-source framework like Express.js and Koa.js. It was created by the mobile team at Walmart Labs - led by Eran Hammer - to handle heavy traffic for events like Black Friday.

Why Hapi.js?

According to Eran Hammer, who is the creator of Hapi.js, there are several reasons why you consider Hapi.js (Why you should consider Hapi.js)

Readability vs Performance

Eran chooses the code readability instead of the performance. Readability and performance are in conflict. The performance keep getting better and faster as machines get improved. However, most issues were found and resolved easily since the codes kept being readable. This point lets him choose the code readability in terms of 'Humans' performance.

Minimal Dependencies

If you followed recent news, this is a big deal

He created Hapi.js without any external dependencies except for one external dependency. I think he really cares about keeping Hapi.js to be maintainable and preventing it from being crashed down by external dependencies. He gives this link and you feel why he considers not to introduce external dependencies.

100% Code Coverage and Style

Hapi was required to have 100% test code coverage. Hapi Team wrote their own test code when existing test tools are not enough to cover. And they keep revising the style as better approaches are developed and all code bases are introduced with the new approaches.

Less Open Issues

There are 27 modules for the entire framework. Except for Joi module, the rest of the modules have 6 open pull requests, 9 open reported issues and 19 open feature requests or questions. This is a significant low number of reporting issues.

Comparison Hapi.js and Express.js

Since I have learned Express.js, my perspective was stick to Express.js syntax and had negative thoughts on learning new Node.js framework. But after I've learned Hapi, my perspective has changed in a good way.

1. Easier to read code

I realize that Hapi.js is somewhat wordier than Express.js, but Hapi.js explicitly describes url, request method, request body, and params. This characteristic is powerful when APIs are getting growing and complex. Because we don't need to take a look at what query or request body looks like.

  /**
    Assuming that you create getUser() and loginUser() method.
  */

  /* #### Express.js #### */
  // GET /user
  // Params: id
  app.get('/user', getUser);

  /* #### Hapi.js #### */
  server.route({
    method: 'GET',
    path: '/user',
    config: {
      handler: getUser,
      validate: {
        query: {
          id: Joi.string()
        }
      }
    }
  })

  /*  =======================================  */

  /*  #### Express.js #### */
  // POST /user
  // Body: username, password
  app.post('/user', loginUser);

  /*  #### Hapi.js ####   */
  server.route({
      method: 'POST',
      path: '/user',
      config: {
        handler: loginUser,
        validate: {
          payload: {
            username: Joi.string(),
            password: Joi.string()
          }
        }
      }
    })
Enter fullscreen mode Exit fullscreen mode

2. Provided data validation by default

Hapi.js provides you with type validation for input from client with Joi package. When invalid input comes from HTTP Request to certain endpoints, the endpoints throw the error and block to execute handler method at the request level.

  server.route({
      method: 'POST',
      path: '/user',
      config: {
        handler: loginUser,
        validate: {
          payload: {
            username: Joi.string(),
            password: Joi.string()
          }
        }
      }
    })
Enter fullscreen mode Exit fullscreen mode

Above example, if username is 123234 which is number, that endpoint throw the error and block to execute loginUser method. So it is much safer and easier to find what invalid data comes in.

Conclusion

Hapi.js gives me chances to learn other Node.js Framework and the pursuit of Hapi.js. I feel like the creator of Hapi.js really understand how developers feel when they work. I don't know much about Hapi.js so there probably are lots of features to comfort developers.

I am coming back to the next article soon!!

References:

Discussion (0)