Why I Like Hapi More Than Express
Why I Like Hapi More Than Express
MENU
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 1/14
9/29/2016 Why I like Hapi more than Express
But Hapi beats it hands down. Hapi, out of the box has:
4. Better testability
Note: I'm using ES6 and HTTPie because it's my blog and I can do
what I want.
Hapi
const Hapi = require('hapi')
server.route({
method: 'POST',
path: '/hello',
handler: (req, reply) => {
reply({ hello: req.payload.name })
}
})
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 2/14
9/29/2016 Why I like Hapi more than Express
Express
const express = require('express')
const bodyParser = require('body-parser')
app.use(router)
app.listen(port)
Easier Setup
These are pretty similar. But already I see things I don't like in
Express. It requires an additional dependency to parse out my
JSON body, which is annoying. What else would I be POSTing? A
form? That also requires the body-parser dependency!
Hapi, on the other hand, parses both out of the box just ne.
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 3/14
9/29/2016 Why I like Hapi more than Express
$ npm start
Awesome.
$ npm start
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 4/14
9/29/2016 Why I like Hapi more than Express
address: '0.0.0.0',
port: 8080 }
Express?
$ npm start
events.js:141
throw er; // Unhandled 'error' event
^
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 5/14
9/29/2016 Why I like Hapi more than Express
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
app.listen(port, () => {
console.log('Server running at:', port)
}).on('error', err => { console.log('Err!', err) } )
$ http 'http://localhost:8080/random'
HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 19
Content-Type: text/html; charset=utf-8
Date: Tue, 20 Oct 2015 20:35:41 GMT
X-Content-Type-Options: nosniff
X-Powered-By: Express
Hapi?
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 6/14
9/29/2016 Why I like Hapi more than Express
$ http 'http://localhost:8080/random'
HTTP/1.1 404 Not Found
Connection: keep-alive
Date: Tue, 20 Oct 2015 20:35:29 GMT
Transfer-Encoding: chunked
cache-control: no-cache
content-encoding: gzip
content-type: application/json; charset=utf-8
vary: accept-encoding
{
"error": "Not Found",
"statusCode": 404
}
Oh, thank you. (Hapi uses Boom for its HTTP errors, which is
an excellent package).
Moving on, let's look at the startup options. In Express, you can
set some options when you create the server:
$ npm start
Not a peep.
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 8/14
9/29/2016 Why I like Hapi more than Express
},
debug: {
log: 'error'
}
})
$ npm start
/Users/ethan/Documents/ethanmick/hapi-vs-
express/hapi/node_modules/hapi/node_modules/hoek/lib/ind
ex.js:731
throw new Error(msgs.join(' ') || 'Unknown error');
^
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 9/14
9/29/2016 Why I like Hapi more than Express
Wow! You got really angry. But I love it! It even shows you
where the issue was, marked by the [1]. As you x each issue in
your con guration, the error message catches each one:
Again, behind the scenes, they are using their own powerful Joi
module, which you should de nitely take a look at.
Replying
Express favors a response object that has many methods on it,
such as .json or .sendFile . Hapi does two things here. One,
their reply interface is rst and foremost a function.
reply({hello: 'Everyone!'})
1. null
2. unde ned
3. string
4. number
5. boolean
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 10/14
9/29/2016 Why I like Hapi more than Express
6. Bu er object
8. Stream object
9. Promise object
Nice! You can return a Stream and it'll stream the data back to
the user. You can also return a Promise, and when ful lled, it
will send the result to the user. It makes for some beautiful and
elegant code.
Testing
Lastly, I want to touch on testing. Tests are extremely
important, and one thing I've found very hard is to test the
controller part of my code. I have all my logic separated into a
model layer, but I still want to ensure my controllers have code
coverage and work as expected.
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 11/14
9/29/2016 Why I like Hapi more than Express
var req = {
method: 'POST',
url: '/hello',
payload: JSON.stringify({name: 'Ethan'})
};
Hapi has a built in inject method that allows you to, well,
inject an HTTP request into the server. It gets routed like a
normal request to the correct route handler, which then
executes as normal. The result object in our test has the
information that we can validate in a test. You can see how I
fully test a server in my example hapi-starter.
Last Thoughts
There are some other things which I really like about the Hapi
Project. I like how aggressively they move forward. They just
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 12/14
9/29/2016 Why I like Hapi more than Express
They supported IO.js from the very beginning and when Node 4
came out they released Hapi 10:
So there you have it. I've been using Hapi for over 2 years now
and always have been impressed with it. It's de nitely worth a
try for your next project!
Code
Discuss on Hacker News
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 13/14
9/29/2016 Why I like Hapi more than Express
https://www.ethanmick.com/why-i-like-hapi-more-than-express/ 14/14