This course enables developers to focus on writing reusable application logic instead of spending time building infrastructure. It is a rich framework for building applications and services
Installing steps: The Complete hapiJS Course – Go From Beginner To Advanced Chandigarh
Create a new directory myproject, and from there:
- Run: npm init and follow the prompts, this will generate a package.json file for you.
- Run: npm install –save hapi this installs hapi, and saves it in your package.json as a dependency of your project.
That’s it! You now have everything you need in order to create a server using happy.
- Now that we have a server we should add one or two routes so that it actually does something.
- Save the above as server.js and start the server with the command node server.js.
- Now you’ll find that if you visit http://localhost:3000 in your browser.
- You’ll see the text Hello, world!, and if you visit http://localhost:3000/stimpy you’ll see Hello, stimpy!.
- Note that we URI encode the name parameter, this is to prevent content injection attacks.
- Remember, it’s never a good idea to render user provided data without output encoding it first!
- The method parameter can be any valid HTTP method, array of HTTP methods, or an asterisk to allow any method.
- The path parameter defines the path including parameters.
- It can contain optional parameters, numbered parameters, and even wildcards. For more details, see the routing tutorial.
Steps Creating static pages and content
To install inert run this command at the command line: npm install –save inert. This will download inert. Add it to your package.json, which documents which packages are installed.
- We’ve proven that we can start a simple Hapi app with our Hello World application.
- Next, we’ll use a plugin called inert to serve a static page. Before you begin, stop the server with CTRL + C.
- Authentication and Authorization with happpyArticle on how to secure your hapi application.
- How to create a REST API with hapiTutorial about how to create a RESTful API with the Plugins Dogwater (Waterline ORM) and Bedwetter.
- Building a Chat Application with hapi.js, Socket.io and RedisReal-time chat application built using hapi.js, Socket.io and Redis Pub/Sub with end-to-end tests.
- Using hapi.js with Socket.ioTutorial on how to integrate Socket.io with hapi.js
Add the following to your server.js file:
- The server.register() command above adds the inert plugin to your Hapi application.
- If something goes wrong, we want to know, so we’ve passed in an anonymous function which if invoked will receive err and throw that error.
- This callback function is required when registering plugins.
- The server.route() command registers the /hello route, which tells your server to accept GET requests to /hello and reply with the contents of thehello.html file.
- Start up your server with npm start and go tohttp://localhost:3000/hello in your browser.
- Oh no! We’re getting a 404 error because we never created a hello.html file.
- You need to create the missing file to get rid of this error.
- Create a folder called public at the root of your directory with a file calledhello.html within it.
- Inside hello.html put the following HTML: Hello World.. Then reload the page in your browser.
- You should see a header reading “Hello World.”
- Inert will serve whatever content is saved to your hard drive when the request is made.
- Which is what leads to this live reloading behavior.
- Customize the page at /hello to your liking.
Steps to Create a server
The most basic server looks like the following:
- First, we require hapi.
- Then we create a new hapi server object.
- After that we add a connection to the server, passing in a port number to listen on.
- After that, start the server and log that it’s running.
- When adding the server connection, we can also provide a hostname, IP address.
- Or even a Unix socket file, or Windows named pipe to bind the server to.
- For more details, see the API reference.
A common desire when creating any web application, is an access log. To add some basic logging to our application. Let’s load the good plugin and itsgood-console reporter on to our server.
We also have a Gitter instance as well (GitHub account required).
hapi’s stability and reliability is empowering these companies today. If you’re using hapi too we’d love to hear from you.
Gists and code samples
- Walmart Mobile node.js setupAn insight into how hapi.js is used at Walmart
- Code style tipsSimple node.js code style tips to improve code quality
Projects built with hapi.js
- PostmilePostmile is a collaborative list making tool built using hapi.js, Node.js, and MongoDB.
- npm/newwwThe npm website – http://npmjs.com
- ColonizersA HTML5 multiplayer game, based on the popular board game “Catan” by Klaus Teuber.
- Hapi Days Conf 2014 Videoshapi days is a one day conference that’s focused on hapi and the people that use it.
- Nodevember Conf 2014 Hapi.js WorkshopA Hapi from Scratch Tutorial By Ben Acker And Wyatt Preul at Nodevember Conf.