HapiJS vs KoaJS in Node.js
Last Updated :
06 Jan, 2022
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 that 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
javascript
// Node.js program 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 that 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
JavaScript
// 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.
Similar Reads
Express.js vs KoaJS in Node.js
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming languag
4 min read
Node.js vs Vue.js
Node.js: It is a JavaScript runtime environment, which is built on Chrome's V8 JavaScript engine. It is developed by Ryan Dahl who is a Software Engineer working at Google Brain, he also developed Deno JavaScript and TypeScript runtime. Node.js is cross-platform and open-source which executes JavaSc
3 min read
HTTP Request vs HapiJS Request in Node.js
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming languag
3 min read
Node.js Basics
NodeJS is a powerful and efficient open-source runtime environment built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side, which helps in the creation of scalable and high-performance web applications. In this article, we will discuss the NodeJs Basics and
7 min read
What are modules in Node JS ?
In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
What are Modules in Node.js ?
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
Node.js vs Java
Node.js: Node.js a library that is used to create runtime applications. It was initially written by Rayn Dahl for using the JavaScript outside the browser and later on it was managed by Joyent. Node.js is used for both front-end and back-end and developers can build mobile applications as well. With
3 min read
What is REST API in NodeJS?
NodeJS is an ideal choice for developers who aim to build fast and efficient web applications with RESTful APIs. It is widely adopted in web development due to its non-blocking, event-driven architecture, making it suitable for handling numerous simultaneous requests efficiently.But what makes NodeJ
7 min read
Types of API functions in Node.js
Node.js, known for its asynchronous and event-driven architecture, is a popular choice for building scalable server-side applications. One of its primary uses is to create and manage APIs (Application Programming Interfaces). APIs allow different software systems to communicate and share data with e
6 min read
Node.js API Monitoring Tools
Software developers are turning their eyes on the popular JavaScript framework NodeJS for all further developments into software solutions. The hike in demand for this technology is creating a need for monitoring the performance of applications, servers, and all-important metrics. Let us look at som
3 min read