Exploring the Node.js HTTP Module Part 9

Опубликовано: 15 Август 2026
на канале: MBCode
20
0

Exploring the node.js http module. The http module is one of many core node.js modules and the node.js runtime environment. Its main purpose is to create http servers so that we can transfer data over the http protocol, which we know as the web. Now, the http module has a few properties and methods and five major classes. Now the 43 methods to see the other, we create server to get. And request methods are quite popular and those are widely used in the application. But the other three properties, the global agent methods and status codes, they're just going to show you what they are but we're not going to use them in the program at all. And the five classes on the right side you see are the agent, client requests, servers, server, response, incoming message. These are the classes that are responsible for creating objects that will be passed to the server to process some data. So let's go back to this very popular client server side model. Again, just to make sure we have a good grasp of what things are, how they're laid out in the rain environment. So here we have the client side, a user. And then on the service side now when we created no HDP server, it will live on the server side. And again when a client sends the request over to the service side, the node js http server, will we see the request and passes data to a request object. That object will send the data to the event loop if there are any long term operations that gets sent back to the thread pool process gets processed the back. When it comes back to the callback function, then again it pass the data to a response object which is in turn sends that off to the browser. So this is a typical model we see all the time. So when we create this http server we're going to do the following which is quite simple really. So we can our app Js and then the Js we're going to import the http module. Once we imported that module we're going to create a server. So critical to create server function with earlier going to pass in an arms function where a callback function that takes two parameters. The first one is the request and then the second response in that order and the inside this function we will process the data. So again this is a very low level. That means you are responsible for writing the content type, what type of data is going to be divisible to the browser and so on. So you have to do that. And then we can then process that you are all based on the particular pattern or we call this the routing pattern. And we display the data to the user and we're going to end that request, the response we issued at the end of function down there. But now it's going to happen until we set this server to listen to a particular ports. So we listened to a poor 3000, for example. And so this is how a an http server is created in node js. In the next video, we're going to actually create this http server and node js.