One of the key features of Node.js is its extensive module system, which provides various modules to enhance and simplify web development. In this article, we will delve into the Web module in Node.js and explore its capabilities.
What is Web Server?
A web server is a software application that serves as the foundation for hosting and delivering websites, web applications, and other online content over the internet. It acts as a mediator between a client (such as a web browser) and the requested web resources, facilitating the exchange of information between them.
The primary function of a web server is to handle and respond to incoming HTTP (Hypertext Transfer Protocol) requests from clients. When a user enters a URL in their browser or clicks on a hyperlink, the browser sends an HTTP request to the appropriate web server. The web server processes the request, retrieves the requested resources (such as HTML files, images, or data), and sends a response back to the client.
Here are some key aspects and functionalities of a web server:
- Routing and Resource Handling
- HTTP Request Processing
- Response Generation
- Content Delivery
- Security and Authentication
- Performance and Scalability
Examples of popular web servers include Apache HTTP Server, Nginx, Microsoft IIS (Internet Information Services), and Node.js (when used as a web server). These servers often come with additional features and modules that extend their functionality, such as URL rewriting, proxying, compression, and more.
Nodejs Web Module
The Web module, also known as the HTTP module, is a core module in Node.js that enables developers to create web servers and handle HTTP requests and responses. It provides a straightforward API for building robust web applications. Let’s dive into the key components and functionalities of the Web module.
Setting Up a Basic Web Server:
To get started, we need to import the Web module using the ‘require’ statement:
const http = require('http');
Next, we can create a basic web server using the ‘http.createServer()’ method:
const server = http.createServer((req, res) => {
// handle requests here
});
The ‘createServer()’ method takes a callback function that will be executed whenever a request is made to the server. Inside the callback, we can define how the server should respond to different types of requests.
Handling Requests and Sending Responses in nodejs:
The callback function in the ‘createServer()’ method receives two parameters: ‘req’ (the incoming request) and ‘res’ (the outgoing response). We can use these objects to handle requests and send appropriate responses.
Let’s look at an example that sends a simple “Hello, World!” response for all requests:
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!');
});
In this example, we set the status code to 200 (indicating a successful response), set the ‘Content-Type’ header to text/plain, and send the response body using the res.end() method.
Starting the Server:
Once we have created the server, we need to start it to listen for incoming requests. We can specify the port number on which the server should listen:
const port = 3000;
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
In this case, the server will listen on port 3000. You can change it to any available port of your choice.
Handling Different Routes:
To handle different routes, we can examine the ‘req.url’ property and define separate logic for each route. Let’s consider an example where we have two routes: ‘/home’ and ‘/abou’t:
const server = http.createServer((req, res) => {
if (req.url === '/home') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Welcome to the home page!');
} else if (req.url === '/about') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('This is the about page.');
} else {
res.statusCode = 404;
res.end('Page not found.');
}
});
In this example, we check the value of ‘req.url’ and send different responses accordingly. If the route is not found, we set the status code to 404 and send an appropriate message.
Conclusion:
The Web module in Node.js provides a powerful API for building web servers and handling HTTP requests and responses. In this article, we explored the basics of creating a web server, handling requests, and sending responses.

