Requesting in http vs Requesting in Express.js
Last Updated :
21 Mar, 2023
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 often use Node.js for building back-end services like APIs like Web App or Mobile App.
http Module:
Import http module: Import http module and store returned HTTP instance into a variable.
Syntax:
const http = require("http");
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
const http = require( "http" );
const PORT = process.env.PORT || 2020;
const server = http.createServer(
function (req, res) {
res.write( 'Hello geeksforgeeks!' );
res.end();
}
)
.listen(PORT, error => {
console.log(`Server listening on port ${PORT}`)
});
|
Steps to run the application:
Run the index.js file using the following command:
node index.js
Output:
Server listening on port 2020
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 the express module and store returned instance into a variable.
Syntax:
const 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
const 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 that help 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 2: 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:
});
|
Steps to run the application:
Run the 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.
Similar Reads
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
How to Use Handle Get Request in Express.js ?
Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in
3 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
How to receive post parameter in Express.js ?
Express is a small framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing; it adds helpful utilities to Node.jsâs HTTP objects; it facilitates the
3 min read
What is routing in Express?
In web applications, without routing it becomes difficult for the developers to handle multiple different requests because they have to manually process each URL request in a single function this problem was solved by the express router which provides a structured way to map different requests to th
5 min read
Express.js req.range() Function
The req.range() function is basically a Range header parser where the Accept-Ranges response-header field allows the server to indicate its acceptance of range requests for a resource. Syntax: req.range(size[, options]) Parameter: size: It is the maximum size of the resource.options: It is an object
2 min read
How To Make A GET Request using Postman and Express JS
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how To Make A GET Request using Postman and Express JS PrerequisitesNode JSExpress JSPostmanTable of Content What is GET Request?Steps to make a GET Request
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
How to intercept response.send() / response.json() in Express JS
In the context of Express , "intercept" usually refers to the process of capturing or modifying a request or response in a middleware function before it reaches its final destination (e.g., a route handler) or before it is sent back to the client. In Express, intercepting response.send() or response
3 min read
Understanding the Function of a Wildcard Route in Express.js
In this article, we are going to learn about the Wildcard Route and its functions. Wildcard routes are the default case route. If a client requests a route that does not exist on the server, that particular route will be routed to the wildcard route; it is also known as the Error 404 route. Approach
2 min read