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
const Hapi = require( '@hapi/hapi' );
const server = Hapi.server({
port: 2020,
host: 'localhost'
});
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
const koa = require( 'koa' );
const app = new koa();
const PORT = process.env.PORT || 2020;
const IP = process.env.IP || 2021;
app.use( function *() {
this .body = "Hello GeeksForGeeks!";
});
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
Express vs Hapi in Node.js
'Express' Module: In order to use the express 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 express --save // OR >> npm i express -s Import express mod
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 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
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
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
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
Explain V8 engine in Node.js
The V8 engine is one of the core components of Node.js, and understanding its role and how it works can significantly improve your understanding of how Node.js executes JavaScript code. In this article, we will discuss the V8 engineâs importance and its working in the context of Node.js. What is a V
7 min read
Source Mapping in Node.js
In this article, we will learn about the source mapping techniques that happen in the Node.js web framework that uses JavaScript. The source maps basically represent the data transmission file from the source of origin of the message to the target destination of the function. The JSON files in the J
4 min read