Express vs Hapi in Node.js
Last Updated :
13 Dec, 2021
‘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 module: Import express module and store returned instance into a variable.
Syntax:
var express = require("express");
Creating Server: The above syntax calls the “express()” function and creates a new express application which gets stored inside the app variable.
Syntax:
const app = express();
// OR Importing and creating express application
var express = require("express")();
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 Express.js module in Node.js.
Example 1: Filename: index.js
javascript
const express = require( 'express' );
const app = express();
const PORT = process.env.PORT || 2020;
const IP = process.env.IP || 2021;
app.get( '/' , (req, res) => {
res.send( 'Hello Vikas_g from geeksforgeeks!' );
});
app.get( '*' , (req, res) => {
res.send( 'OOPS!! The link is broken...' );
});
app.listen(PORT, IP, () => {
console.log(`The Server is running
at: http:
});
|
Run index.js file using the following command:
node index.js
Output:
The Server is running at: http://localhost:2020
Now type http://127.0.0.1:2020/ OR http://localhost:2020/ in a web browser to see the output.
‘Hapi’ Module: In order to use the hapiJS module, we need to install the NPM (Node Package Manager) and the following modules (on cmd).
// Creates 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‘, ‘geeksforgeeks.org’, etc.
The below example illustrates the Hapijs module in Node.js.
Example 2: 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.
Share your thoughts in the comments
Please Login to comment...