HTTP Module Introduction in Node.js
The Node.js HTTP module allows us to create web servers, receive HTTP requests from clients, and send HTTP responses. It is a built-in module, so we can use it without installing an external package.
1. What is the HTTP Module?
HTTP stands for Hypertext Transfer Protocol.
It defines how clients and servers communicate over the web.
Node.js provides the built-in http module to create
HTTP servers and handle requests and responses.
Imagine Samadhan opens the CIIT Training Institute website in his browser. His browser sends an HTTP request to the server. The server processes the request and sends back an HTTP response containing information such as a web page or text message.
2. Why Do We Use the HTTP Module?
- To create an HTTP server using Node.js.
- To receive requests from browsers and other clients.
- To inspect request URLs and HTTP methods.
- To send responses such as text, HTML, and JSON.
- To build simple APIs and web-server applications.
3. How Client and Server Communicate
4. Importing the HTTP Module
The http module is built into Node.js.
In a CommonJS JavaScript file, we can import it using
require().
const http = require("http");
console.log("HTTP module loaded successfully.");
Output
HTTP module loaded successfully.
Explanation
require("http")loads the built-in HTTP module.const httpstores the imported module.- We can use the
httpobject to create a server. - No separate npm package installation is required for this module.
5. Creating a Basic HTTP Server
We can create an HTTP server using
http.createServer(). It accepts a callback that receives
the incoming request and outgoing response objects.
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/plain"
});
res.end("Welcome to CIIT Training Institute!");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Console Output
Server running at http://localhost:3000
Browser Output
Explanation
http.createServer()creates an HTTP server.reqrepresents the incoming request.resrepresents the response sent by the server.res.writeHead(200, ...)sets the status code and response content type.res.end()sends the response body and completes the response.server.listen(3000)starts listening on port 3000.
6. Understanding req and res
| Object | Meaning | Common Usage |
|---|---|---|
req |
Incoming HTTP request. | req.url, req.method, req.headers |
res |
Outgoing HTTP response. | res.writeHead(), res.write(), res.end() |
7. HTTP Server Execution Flow
8. How to Run the HTTP Server
Follow these steps to execute the example on your computer.
- Install Node.js if it is not already installed.
- Create a file named
server.js. - Paste the basic HTTP server code into that file.
- Open a terminal in the folder containing
server.js. - Run the following command.
node server.js
After the server starts, open this address in your browser:
http://localhost:3000
9. Important HTTP Module Methods
| Method / Property | Purpose |
|---|---|
http.createServer() |
Creates an HTTP server. |
server.listen() |
Starts listening for incoming connections. |
req.url |
Provides the request URL path and query string. |
req.method |
Provides the HTTP method, such as GET or POST. |
res.writeHead() |
Sets the response status code and headers. |
res.write() |
Writes part of the response body. |
res.end() |
Completes the response. |
10. Common Mistakes
| Mistake | Correct Approach |
|---|---|
Forgetting to call server.listen(). |
Start the server on an available port. |
| Forgetting to finish the response. | Call res.end() when the response is complete. |
| Using the wrong content type. | Set an appropriate header, such as text/plain for plain text. |
| Choosing a port that is already occupied. | Use an available port or stop the conflicting process. |
| Expecting the browser to show the terminal message as page content. | Use console.log() for terminal output and res.end() for browser response content. |
11. Important Points to Remember
- The HTTP module is built into Node.js.
http.createServer()creates a server.reqcontains incoming request information.resis used to send the response.server.listen()starts listening on a port.res.end()completes the response.- The built-in HTTP module can be used to create basic web servers and APIs.
Summary
In this lesson, we learned what the Node.js HTTP module is, why it is used, how clients and servers communicate, and how to create a basic HTTP server. We also explored request and response objects, important HTTP methods, server execution, and common mistakes using examples from CIIT Training Institute involving Samadhan and Pradnya.