Day 10 of NodeJS || HTTP module|| Part 2

Hey reader👋Hope you are doing well😊
In the last post we have discussed about introduction to HTTP module in NodeJS. In this post we are going to discuss about some functionalities of HTTP module in NodeJS.
So let’s get started🔥

Create Server …


This content originally appeared on DEV Community and was authored by Akshat Sharma

Hey reader👋Hope you are doing well😊
In the last post we have discussed about introduction to HTTP module in NodeJS. In this post we are going to discuss about some functionalities of HTTP module in NodeJS.
So let's get started🔥

Create Server using HTTP

A server is a computer or system that provides resources, data, services, or programs to other computers, known as clients, over a network.

To create server using HTTP module we use createServer() method.
This is an asynchronous method used to create server. It takes a callback function that accepts two arguments "request" and "response". The "request" object is responsible for handling any incoming request to page and "response" object will give us appropriate response.
Image description
So here you can see that we have created a server using createServer(). Whenever we get a request our server accepts it and will give response to us. Here we are getting "hello world" as response at our end point.
Let's understand request and response in more better way -:

Request Object

The req object represents the incoming HTTP request. It contains all the information about the client's request, such as the URL, HTTP method (GET, POST, etc.), headers, and any data sent from the client (e.g., form data, JSON payload).

Key properties and methods of the req object:

  • req.url: The path part of the request URL.

  • req.method: The HTTP method used (e.g., GET, POST).

  • req.headers: An object containing the request headers.

  • req.query: An object containing query string parameters (if using a query string parsing library).

  • req.body: The body of the request, typically parsed with middleware like body-parser.

  • req.on('data', callback): Listens for data chunks being sent in the request body (useful for processing streams of data).

Response Object

The res object represents the server's response that will be sent back to the client. You use this object to set headers, HTTP status codes, and to send the actual content (HTML, JSON, etc.) back to the client.

Key properties and methods of the res object:

  • res.statusCode: Sets the HTTP status code (e.g., 200, 404, 500).

  • res.setHeader(name, value): Sets a specific HTTP header.

  • res.write(data): Sends a chunk of the response body. Useful for streaming data.

  • res.end([data]): Signals that the response has been completed.
    Optionally, you can pass the final chunk of data to be sent.

  • res.writeHead(statusCode, headers): Sets the status code and headers together.

Now you might be finding it difficult to understand these headers and status codes. Don't worry I'll write another blog which will give you thorough explanation of these.

Now to display response the server should listen on particular port.

A port is a 16-bit number ranging from 0 to 65535 that identifies a specific process or service on a device. It allows multiple network services to coexist on a single machine.

Image description
So here we are using listen() method that is going to handle any incoming request to our server on port 8080. Once the server is successfully listening, the provided callback function is executed, which logs a message to the console to indicate that the server is running and ready to accept connections.
To run the code use command node script.js, then open http://localhost:8080/ on your web browser you will see the desired response on your web page.
Image description
Image description

So this is how you can create server using http module. I hope you have understood it well. In the next blog we will know more about this module. Till then stay connected and don't forget to follow me.
Thankyou 🤍


This content originally appeared on DEV Community and was authored by Akshat Sharma


Print Share Comment Cite Upload Translate Updates
APA

Akshat Sharma | Sciencx (2024-08-09T12:41:19+00:00) Day 10 of NodeJS || HTTP module|| Part 2. Retrieved from https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/

MLA
" » Day 10 of NodeJS || HTTP module|| Part 2." Akshat Sharma | Sciencx - Friday August 9, 2024, https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/
HARVARD
Akshat Sharma | Sciencx Friday August 9, 2024 » Day 10 of NodeJS || HTTP module|| Part 2., viewed ,<https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/>
VANCOUVER
Akshat Sharma | Sciencx - » Day 10 of NodeJS || HTTP module|| Part 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/
CHICAGO
" » Day 10 of NodeJS || HTTP module|| Part 2." Akshat Sharma | Sciencx - Accessed . https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/
IEEE
" » Day 10 of NodeJS || HTTP module|| Part 2." Akshat Sharma | Sciencx [Online]. Available: https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/. [Accessed: ]
rf:citation
» Day 10 of NodeJS || HTTP module|| Part 2 | Akshat Sharma | Sciencx | https://www.scien.cx/2024/08/09/day-10-of-nodejs-http-module-part-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.