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.
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 vs Express
Node.js and Express are two essential tools in the JavaScript ecosystem, especially for server-side development. While they are often used together, they serve different purposes. This article will explain what Node.js and Express are, their key differences, and when to use each. Table of Content No
5 min read
HapiJS vs KoaJS in Node.js
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 re
3 min read
Globals View in Express JS
Express.js is a popular web framework for Node.js that simplifies the process of building web applications and APIs. One of the features provided by Express.js is the ability to define and use global variables within your application's views. In this article, we'll explore what globals views are, ho
4 min read
Express.js | router.use() Function
The router.use() function uses the specified middleware function or functions. It basically mounts middleware for the routes which are being served by the specific router. Syntax: router.use( path, function )Parameters: Path: It is the path to this middleware, if we can have /user, now this middlewa
2 min read
Express.js | res.format() Function
The res.format() function performs content negotiation on the Accept HTTP header on the request object if it is present. This function checks the Accept HTTP request header and then invokes the corresponding handler depending on the Accept value. Syntax: res.format(object) Installation of the expres
2 min read
Requesting in http vs Requesting in Express.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 Node.js is not a framework and itâs not a programming language. Most people are confused and understand itâs a framework or a programming language. We
3 min read
Express.js res.set() Function
The res.set() function is used to set the response HTTP header field to value. To set multiple fields at once, pass an object as the parameter. Syntax: res.set(field [, value])Parameters: The field parameter is the name of the field and the value parameter is the value assigned to the field paramete
2 min read
What is Express-rate-limit in Node.js ?
express-rate-limit is a middleware for Express.js applications that helps control the rate at which requests can be made to the server. It is particularly useful for preventing abuse by limiting the number of requests a client can make to your API within a specific time frame. This can help mitigate
3 min read
Express res.json() Function
The res.json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.stringify() method. Syntax: res.json( [body] )Parameters: The body parameter is the body that is to be sent in the response. Ret
2 min read