In this tutorial, we’ll learn about creating a hapi server. We’ll also learn about routing in Hapi and how simple it is to set up a server in seconds.
Creating A Server
Let’s just get right to it. If you haven’t already, clear out your index.js file and add this bit of code.
var Hapi = require('hapi'); var server = new Hapi.Server('localhost', 8080); server.start(function() { console.log("Hapi server started @ " + server.info.uri); });
We’re doing a couple of things here. First, we’re requiring our hapi module. How does node know where the hapi module is? Node.js is pretty smart and will try looking for your modules in certain locations. First it’ll look for the module in the same directory as the script. If it doesn’t find the file, then it’ll look in the node_modules folder. If it doesn’t find anything there, then it’ll check if the module is globally available. If it doesn’t find anything, then you’ll get an error.
If we wanted to, we could type in the directory where the module is installed, but we won’t do that since it already looks for it in the node_modules folder. We now have access to all the features Hapi provides out of the box.
We define a variable called server and set it’s value to a new Hapi server. The server() method takes in a couple of parameters. The first 2 are required. We’ll discuss the other parameters in a future tutorial. We first must pass in the hostname or the IP of the server we’re using. We use localhost since w’ere working locally. Next, we pass in the port number. We’ll be using port 8080 again.
Lastly, we just have to start our server by using the start() method. This method takes in a function that allows us to run any code. This is useful if you would like to set up some things before the server starts. We just log a message onto the console which tells us that the server successfully started. The server object provides us properties and methods related to our server.
One of these properties of the info property which gives us information about our own server.
Why aren’t we using the http module like last time?
Hapi already uses this module for you. Yep, that’ right! Modules can require their own modules and those modules can require their own modules. However, you can’t use this module yourself. Why? Node.js doesn’t believe you should be using global variables. So, the http module isn’t available for you to use personally. You could require it if you’d like, but there’s no need to do that.
You can now start your server. Try accessing the home page by going to http://localhost:8080/. You will see an error JSON object outputted onto your browser. We haven’t created any routes. Let’s create one now.
Routing
Hapi takes care of filtering URLs for you. All you have to do is define the path and the method. Let’s take a look at how to create our own routes. Update your script to the code below.
var Hapi = require('hapi'); var server = new Hapi.Server('localhost', 8080); server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply('My Blog'); } }); server.start(function() { console.log("Hapi server started @ " + server.info.uri); });
We use the route() method of the server object. This method has 1 parameter which is an object containg methods and properties for how this route should be handled.
We must pass in a HTTP method. This can be GE’, POST, PUT, PATCH, DELETE, and OPTIONS. If you’re not sure of a method, then you could use * which accepts any method. Next, we must define the URL the user must type in order for them to view this route. In our case, we want to create a route for the home page. The last required option we need to create is the handler method.
This is a function that will run whenever a visitors accesses this page. Hapi will pass in 2 objects to this method which is the request object and the reply object. These objects ARE NOT the same objects given by the http module. They are modified versions of the request and response objects. These 2 objects give us improved properties and methods that will help us create pages. Right now, we’re only outputting My Blog.
Last time we had to use the response.write() method repeatedly to output HTML. This can be extremely tedious if we had multiple pages with hundreds of lines of HTML. Luckily, Hapi provides a way for us to separate code and design by providing something called views.
Conclusion
In the next tutorial, we’ll install a new module called handlebars and learn a little more about views. You’ll start seeing the true power of Hapi soon! If you would like to learn more about Hapi and it’s APIs, then check out this page.