hapi.js - Up and running

Mike Frey
InstructorMike Frey
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-convention makes it the perfect match for any size development team. This video will introduce you to hapi by showing you the bare minimum needed to run a hapi server.

[00:00] The first thing we need to do is install Hapi. We'll do "npm install hapi --save," which will save it to the package.json file as a dependency. Next, I'll use nodemon to watch for changes to files in my project directory. Nodemon will automatically restart the server any time it detects a changed file.

[00:26] In my index.js file, I need to pull in the Hapi framework by requiring it and assign it to the variable "hapi." Next, I'll create a new instance of the Hapi server class, then add a connection which tells the server which host and port to listen on.

[00:48] To add a basic route, I'll use the server's route method and pass in a configuration object that tells the route how to behave. Every route configuration needs three things, a method that tells the router which HTTP verb or verbs to watch for, a path string for the URL to match, and a handler function to handle a request.

[01:07] The handler function takes two arguments, "request" and "reply." I'll just use "reply" to respond with a string of "Hello Hapi."

[01:18] The last thing we need to do is tell the server to start. Server.start takes a callback function, letting us know that the server has been started. I'll log the connection's URI here. Now, when I refresh the browser, it responds with the string "Hello Hapi."

Nish
Nish
~ 8 years ago

Hey guys, how would I start a https server instead.

Michael
Michael
~ 7 years ago

I wanna know to, like hapijs with with websockets.

Sander
Sander
~ 6 years ago

Is there an update/addendum for HAPI 17?

Jeremias enriquez
Jeremias enriquez
~ 6 years ago

Too old... I can't run the server with this directions, I had to read the documentation

devstackio
devstackio
~ 6 years ago

this is for hapi below version 17 .

^17 removed support for multiple connections among other major breaks

Jon
Jon
~ 6 years ago

THIS IS HORRIBLE! EGGHEAD might as well be showing tutorials on how to make fire 🔥

Gush
Gush
~ 5 years ago

DOS 6.11 course, anyone?

Carlos
Carlos
~ 5 years ago

I just get the PRO subscription and this is the first class I take. how come you guys keep content outdated on a paid site? most of this code doesn't make sense on the last versions of hapi.

Markdown supported.
Become a member to join the discussionEnroll Today