Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HapiJS vs KoaJS in Node.js

  • Last Updated : 14 Sep, 2020

HapiJS Module: In order to use the hapiJS module, we need to install the NPM (Node Package Manager) and the following modules (on cmd).

// Create package.json file
>> npm init 

// Installs hapi module
>> npm install @hapi/hapi --save 

Import hapiJS module: Import hapiJS module and store returned instance into a variable.

Syntax:

var Hapi = require("@hapi/hapi");

Creating Server: The above syntax imports the “Hapi” module and now it creates a server. It communicates the request and response with the client and the server. It requires PORT <number> and host <string> to communicate.

Syntax:



const server = Hapi.server({port: 2020, host: 'localhost'});

Parameter: This method accepts three parameters as mentioned above and described below:

  • PORT <Number>: Ports are the endpoints of communication which helps to communicate with the client and the server.

  • HOST <String>: It accepts the host names like localhost/127.0.0.1, google.com, geeksforgeeks.org, etc. 

The below example illustrates the HapiJS module in Node.js.

Example 1: Filename: index.js




// Node.js programe to create server
// using hapi module
  
// Importing hapi module
const Hapi = require('@hapi/hapi');
  
// Creating Server
   const server = Hapi.server({
       port: 2020,
       host: 'localhost'
   });
  
   // Creating route
   server.route({
       method: 'GET',
       path: '/',
       handler: (request, hnd) => {
           return 'Hello GeeksForGeeks!';
       }
   });
  
const start = async () => {
 await server.start();
   console.log('Server running at', server.info.uri);
};
  
process.on('unhandledRejection', (err) => {
   console.log(err);
   process.exit(1);
});
  
start();

Run index.js file using the following command:

node index.js

Output:

Server running at: http://localhost:2020

Now type http://localhost:2020/ in a web browser to see the output.



KoaJS Module: In order to use the KoaJS module, we need to install the NPM (Node Package Manager) and the following modules (on cmd).

// Creates package.json file
>> npm init 

// Installs express module
>> npm install koa --save  // OR
>> npm i koa -s 

Import KoaJS module: Import KoaJS module and store returned instance into a variable.

Syntax:

var koa = require("koa"); //importing koa module

Creating Server: The above syntax imports the koa module and creates a new koa application which gets stored inside the app variable.

Syntax:

const app = new koa();  // Creating koa application

Sending and listening to the response: It communicates the request and response with the client and the server. It requires PORT <number> and IP <number> to communicate.

app.listen(PORT, IP, Callback);

Parameter: This method accepts three parameters as mentioned above and described below:

  • PORT <Number>: Ports are the endpoints of communication which helps to communicate with the client and the server.
  • IP <Number>: IPs represent IPv4 or IPv6 address of a host or a device.
  • Callback <Function>: It accepts a function.

The below example illustrates the Koa module in Node.js.

Example 2: Filename: index.js




// Node.js program to create server
// with help of Koa module
  
// Importing koa module
const koa = require('koa');
  
// Creating new koa app
const app = new koa();
  
// PORT configuration
const PORT = process.env.PORT || 2020;
  
// IP configuration
const IP = process.env.IP || 2021;
  
app.use(function *() {
 this.body = "Hello GeeksForGeeks!";
});
  
// Server listening to requests
app.listen(PORT, IP, ()=>{
console.log("Server started at port", PORT);
});

Run index.js file using the following command:

node index.js

Output:

The Server is running at port 2020

Now type http://127.0.0.1:2020/ OR http://localhost:2020/ in a web browser to see the output.




My Personal Notes arrow_drop_up
Recommended Articles
Page :