How to install Hapijs

Views

HapiJs require Node js to install first. Install node js then go to install Hapijs Framework

HapiJs is a rich Node js framework for building applications and services.

hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.

If you want to start developing apps or services with Hapi framework you need to follow these steps.

1. First you insure that you have installed the Node Js in your system.

2. Create a project folder where you want to create your app or service

	C:ProjectsHapi

3. Now open the command prompt in this folder (C:ProjectsHapi) and start installing the Hapi framework.

4. First run this command to make a package.json file for project dependency management.

	npm init

5. After creating the package.json file run the following command to install HapiJs framework.

	npm install hapi --save

6. As soon the Hapi will installed it will saved its dependency into the package.json file and all hapiJs framework file will be in node_modules folder.

7. Then create a main app file which can be any name like server.js or index.js or main.js. Whatever you want put the name of file.

8. Write the below code in this file

	 
		'use strict';  
		
		const Hapi = require('hapi');  
		//Create a server with a host and port 
		const server = new Hapi.Server(); 
		server.connection({      
			host: 'localhost',      
			port: 8000  
		});  //Add the route 
	
		server.route({     
			method: 'GET',     
			path:'/hello',      
			handler: function (request, reply) {         
				return reply('hello world');     
			} 
		});  
		
		// Start the server 
		server.start((err) => {     
			if (err) {         
				throw err;     
			}     
			
			console.log('Server running at:', server.info.uri); 
		}); 
	

9. Launch the application by running npm start or node server.js and open localhost:8000/hello in your browser

Here we use 'use strict' because use strict is a new feature of ECMAscript 5 for running the JavaScript in a strict mode.

Strict mode means this provide the error when we are doing some coding error and type define error.

Now then we require the Hapi framework and create the object and connection with host as local host and port as 8000.

Then we create a test route for hello world print.

After creating route we start the server which gives us the info of the server running on which host and port in console.

How To Write and Run Your Firs …

How To Write and Run Your First Program in Node.js. Node.js is a popular open-source runtime environment that can execute JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web b …

read more
Views

Getting Started with PM2, the …

Getting Started with PM2, the Node.js Process Manager. PM2, or Process Manager 2 is an incredibly versatile production process manager written in Node.js.Restarting after crashes: PM2 allows us to keep processes running until the heat death of the un …

read more
Views