Hapi.js Training in Chandigarh
Webtechlearning provide the best training in Hapi.js framework in Chandigarh
A rich framework for building applications and services
Table of Contents
hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
During the last week we’ve closed 21 issues, merged 30 pull requests and pushed 30 commits to master branch.
This tutorial is compatible with hapi v10.x.x.
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 hapi.
Creating 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.
Now that we have a server we should add one or two routes so that it actually does something. Let’s see what that looks like:
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.
Creating static pages and content
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.
To install inert run this command at the command line: npm install –save inert This will download inert and add it to your package.json, which documents which packages are installed.
Add the following to your server.js file:
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. We’ve put the routing callback function inside of registering inert because we need to insure that inert is registered beforewe use it to render the static page. It is generally wise to run code that depends on a plugin within the callback that registers that plugin so that you can be absolutely sure that plugin exists when your code runs.
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: <h2>Hello World.</h2>. 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.
More details on how static content is served are detailed on Serving Static Content. This technique is commonly used to serve images, stylesheets, and static pages in your web application.
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.
Aside from the content that you’ll find on this site, there’s also a lot of community-produced content that can help you learn how to be effective with hapi.js. If you’ve produced some content that you want to see on this list, please send us a pull request.
- hapi.js in ActionPacked with examples, this book takes you from your first simple server through the skills you’ll need to build a complete application. In it, you’ll learn how to build websites and APIs, implement caching, authentication, validation, error handling, and a lot more. You’ll also explore vital techniques for production applications, such as testing, monitoring, deployment, and documentation.
- Developing a hapi EdgeThis book shows you how to build enterprise-quality web applications using the hapi service and application framework. By walking through the creation of a real web application, hapi-plugins.com, you will learn how to configure and start hapi, build out APIs, perform authentication, validation, caching, and so much more. You will also discover tips and tricks used in production hapi deployments regarding plugins, testing, debugging, and security. hapi was developed and is used within Walmart and has been battle tested during the most critical days of the year for e-commerce websites, Black Friday. It has proven itself to not only handle extremely high production loads at a breeze but is also a pleasure to work with. The aim of the framework was to address pitfalls in the other established out there, and it has done so without fail.
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
- aquaA website and user system. Implemented with React and Flux.
- frameA user system API. Bring your own front-end.
- generator-hapi-serviceYeoman generator for hapi web services.
- generator-hapi-styleYeoman generator for scaffolding hapi apps and plugins.
- hapi-dashA boilerplate hapi web and API server example, with frontend dashboard
- hapi-ninjaBoilerplate hapi server example. Node.js, hapi, and Swig.
- mulletBoilerplate hapi Server with Facebook and React
- ruthafrontend stack for hapi (server, api) and Angular (client)
- hanx.jsFull-stack boilerplate with Node.js, hapi, PostgreSQL and AngularJS (MEAN.js port)
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.
- 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
- Authentication and Authorization with hapiArticle 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.
- 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.
Contact for Training: 9915337448