Open In App

HTTP Request vs HapiJS Request in Node.js

Last Updated : 22 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 language. We often use Node.js for building back-end services like APIs like Web App or Mobile App.

 

Creating and Binding Server: Create a server instance using createServer() method and bind it to some port using listen() method.

Syntax:

const server = http.createServer().listen(port)

Parameter: This method (listen()) accepts a single parameter as mentioned above and described below:

  • port <Number>: Ports are in the range 1024 to 65535 containing both registered and Dynamic ports.

The below example illustrates the use of http module in Node.js.

Example 1:   Filename: index.js

javascript




// Node.js program to create 
// http server 
 
// Using require to access http module 
const http = require("http");
 
// Port number
const PORT = process.env.PORT || 2020;
 
// Creating server
const server = http.createServer(
  // Server listening on port 2020
  function (req, res) {
     res.write('Hello geeksforgeeks!'); 
     // Write a response to the client
     res.end(); 
  }
)
.listen(PORT, error => {
  // Prints in console
  console.log(`Server listening on port ${PORT}`)
});


Run index.js file using the following command:

node index.js

Output:

Server listening on port 2020 
 

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

 

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 two 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 2: 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://127.0.0.1:2020/ OR http://localhost:2020/ in a web browser to see the output.



Similar Reads

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 &gt;&gt; npm init // Installs hapi module &gt;&gt; npm install @hapi/hapi --save Import hapiJS module: Import hapiJS module and store returned instance into a variable. Syntax: var Hapi
3 min read
Will the 'error' event ever be emitted on 'http.IncomingMessage' in a node.js http.request ?
In this article, we will see how to handle the 'error' event that is emitted on 'http.IncomingMessage' in a node.js http.request. In Node.js, the 'http.IncomingMessage' object is used to represent the incoming HTTP request message. This object is a readable stream, and it can emit various events such as 'data', 'end', and 'error'. One of the events
3 min read
Why are HTTP cookies used by Node.js for sending and receiving HTTP cookies?
The HTTP protocol is one of the most important protocols of the Application layer of the OSI model. The main use of this protocol is for sending hypertext information to the client to the server and server to the client to communicate on the World Wide Web. But, the HTTP protocol is a stateless protocol which means that this protocol cannot maintai
5 min read
Different types of module used for performing HTTP Request and Response in Node.js
HTTP's requests and responses are the main fundamental block of the World Wide Web. There are many approaches to perform an HTTP request and Response in Node.js. Various open-source libraries are also available for performing any kind of HTTP request and Response. An HTTP request is meant to either retrieve data from a specified URI or to push data
3 min read
Creating Your Own Node HTTP Request Router
In Node, if we want to build our own custom HTTP request router then we can create it by handling the incoming requests and directing them to the specific endpoints. Using this routing process, allows us to define the different actions or responses that are based on the requested URLs. In this article, we will create our own node HTTP request route
3 min read
How to detect AJAX request to normal request in Node.js ?
Ajax stands for asynchronous javascript and XML that is used to make dynamic requests to the server while the client is running. There are two techniques to detect AJAX requests in Nodejs which are Check the headers of the request and if it contains HTTP_X_REQUESTED_WITH having a value of XMLHttpRequest then it is an ajax request.When you make an A
3 min read
Difference between HTTP/2 and HTTP/1.1
HTTP stands for hypertext transfer protocol &amp; it is used in client-server communication. By using HTTP user sends the request to the server &amp; the server sends the response to the user. There are several stages of development of HTTP but we will focus mainly on HTTP/1.1 which was created in 1997 &amp; the new one is HTTP/2 which was created
3 min read
Explain HTTP Request in Backbone.js
Backbone.js is a compact library used to organize JavaScript code. An MVC/MV* framework is another term for it. If MVC is unfamiliar to you, it is just a technique for designing user interfaces. The creation of a program's user interface is made considerably easier by JavaScript functions. BackboneJS provides a variety of building elements to aid d
4 min read
HTTP headers | Access-Control-Request-Headers
Access-Control-Request-Headers is a request-type header used by browsers that contains information about the different HTTP headers that will be sent by the client in the ensuing request. Whenever a client initiates a request to a server, the browser checks if the request needs a CORS preflight or not. In case a CORS preflight request is required,
2 min read
HTTP headers | Access-Control-Request-Method
The HTTP headers Access-Control-Request-Method is a request type header which has been used to inform the server that which HTTP method will be used when the actual request is made. Syntax: Access-Control-Request-Method: &lt;method&gt; Directives: This header accept a single directive which is mentioned above and described below: &lt;method&gt;: Th
1 min read