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.
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.
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.
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 🤍
Top comments (0)