Learn how to execute a simple hello world using Hapi in Node.js.
- [Voiceover] Let's test out the development environment now by creating a simple, Hapi, hello world example. This way we can get everything up and running and make sure that node is working as expected. Start up visual studio Code and open the directory with the exercise files. Open the start for this module, and you'll notice that it is, in fact, an empty file. That's okay, we're going to fill it up, we're going to create a server that will respond to what we want. So the first thing you're going to want to do is you're going to want to set up Hapi.
So we're requiring the Hapi library. And once we've set up the Hapi library, we're going to have to create a server. So we're going to use the Hapi server, here. There are some options that you can set up there, but we're just going to do the default, except, that we want to set up the connection to use a different port, as we're going to be using Port 8080, this way it won't conflict with anything you already have running on your system. Alright, so now we've defined our server, and the server connection.
Before we set up the routes, let's just go ahead and put in the server start command. And so, when the server starts up, it's going to run this function. And what we're going to have the server do when it starts up is just tell us, with a log to the console, that it's started up, so Hapi is listening on local host 8080, so, that's just going to give us some good feedback when we run it to make sure that everything is working okay.
So, let's get to routes. In Hapi, routes are defined as an array of objects, and each object knows what its method is, and what its path is, and it also has a handler. So we're going to set up a very simple route here, and we'll just have an array of one, for now. We'll be adding lots more later. Okay, so, the first thing you always have to have in a route is you always need to have a method. And the method here is going to be get.
We're only going to be listening for those read requests. The next thing is, you're going to set the path. We're just going to do it at the root of the system. And then, we're going to have a handler, and this is a function, which has a request and reply. And the request is information about what the client has sent, and the reply is used to communicate things back to the client. So, what we're going to do here is we're just going to use the reply, and say hello world from Hapi.
And that's it, that's all we need to do, and we can go over to our terminal, and in the directory that we're already in, we run node 00_07.js, and you can see, there's that log to the console that says, Hapi is listening on local host 8080. Great, let's double check and make sure that yes, when I load local host 8080, I get a hello world from Hapi response. So, as you can see, the server is up and running and ready to go, we'll be editing this file in future modules, to add a lot more functionality, but for now, you've got a successful Hapi server working.
- Setting up the development environment
- Consuming an API using HTTPie
- Inspecting Mongo data
- Updating the server for MongoDB read functionality
- Setting up application endpoints
- Implementing user endpoints, progress endpoints, and promises