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
// 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}`)
});
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
// Node.js program to create server
// with help of Express.js module
// Importing express
const express = require('express');
// Creating new express app
const app = express();
// PORT configuration
const PORT = process.env.PORT || 2020;
// IP configuration
const IP = process.env.IP || 2021;
// Create a route for the app
app.get('/', (req, res) => {
res.send('Hello Vikas_g from geeksforgeeks!');
});
// Create a route for the app
app.get('*', (req, res) => {
res.send('OOPS!! The link is broken...');
});
// Server listening to requests
app.listen(PORT, IP, () => {
console.log(
`The Server is running at: http://localhost:${PORT}/`);
});
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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read