Just a disclaimer before we start, I have worked with Express and will definitely not say it is a better framework but just a different one. Hapi.js is a web framework for rapidly building web applications, REST API and services.
Hapi was created around the idea that configuration is better than code, that business logic must be isolated from the transport later. – Erin Hammer
Since, it is quite similar to Express, let us compare the syntax between Express and Hapi for some common functionality that the frameworks will be used to implement.
We require express and then instantiate it by assigning it to the variable app. Then instantiate a server to listen to a port, port 3000. The app.listen() is actually just a wrapper around node’s http.createServer().
hapi is required but instead of instantiating a hapi app, you create a new Server and specify the port. In Express we get a callback function but in Hapi we get a new server object. Then once we call server.start() we start the server on port 3000 which then returns a callback.
Now since the web server is created, let us create a few routes that can be served by the http server.
We are using the get() method to catch the incoming request of “GET /” and then invoke a callback function that handles two parameters req and res. The following are some of the more commonly used methods that are supported by Express (but not all of the methods): get, post, put, head, delete etc.
First impressions of the routes in Hapi are how clean and readable they are compaired to the other frameworks. Even the required options method, path, handler, and replay for the routes are easy to the eye.
I believe this is enough introduction to get you guys started. “Hapi” Coding!