Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 960 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

hapi.js - Up and running

1:40 Node.js lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

Avatar
Nish

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

Avatar
Igor

Thanks for a great course. Are there plans for extended HapiJS course on egghead.io?

In reply to egghead.io

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.

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.

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.

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

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."

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?