DEV Community

Cover image for 4-Nodejs Course 2023: Http Request Life Cycle
Hasan Zohdy
Hasan Zohdy

Posted on

4-Nodejs Course 2023: Http Request Life Cycle

What Is Http Request

Http request basically starts when the user makes a request to the server and ends when the server sends a response back to the user.

In simple terms, the user opens the browser, types the URL and presses enter. The browser then sends a request to the server. The server then processes the request and sends a response back to the browser. The browser then displays the response to the user.

During the request start and response release, the server will/shall make some decisions based on the request, for example to determine what should be done when the user hits that certain route/path, is there something should be done first before processing this request, for example if the user tries to open a profile page, we should first check if the user is logged in or not, if not, we should redirect the user to the login page, and so on, this is called a middleware.

So it all begins with the url, let's get into the definition of the url.

What is URL

URL stands for Uniform Resource Locator. It is a string of characters that identifies a resource on the web. It is used to locate and identify a resource on the web.

URL Structure

URLs are made up of several parts. The following is the structure of a URL:

protocol://domain:port/path?query_string#fragment
Enter fullscreen mode Exit fullscreen mode

Protocol

The protocol is the first part of the URL. It tells the browser what protocol to use to access the resource. The most common protocols are http and https.

There are other protocols like sockets and ftp. The protocol is followed by ://.

Http vs Https

The difference between http and https is that http is unsecured while https is secured. https uses SSL/TLS to encrypt the data being sent between the client and the server.

Domain

The domain is the second part of the URL. It is the name of the server that hosts the resource. It can be an IP address or a domain name.

Port

The port is the third part of the URL. It is the port number that the server is listening on. The default port for http is 80 and for https is 443.

The default port can be omitted from the URL.

Route/Path

The route/path is the fourth part of the URL. It is the path to the resource on the server. It is optional.

This is where we'll start handle our requests based on the route, for example, if the user requests the / route, the user will be navigated to the home page, if the user requests the /about-us route, the user will be navigated to the about page.

Query String

The query string is the fifth part of the URL. It is a set of key-value pairs that are separated by &. It is used to pass data to the server. It is optional.

It starts with a ? and each key and value pair is separated by =. The key and value are separated by &.

Fragment

The fragment is the sixth part of the URL. It starts with #. It is used to identify a specific part of the resource. It is optional.

What is a request?

A request is a message sent by a client to a server. It contains the information about the client and the action the client wants to perform.

What is a request life cycle?

The request life cycle is the process of handling a request from the moment it is received by the server until the moment the response is sent back to the client.

The server will search for the given route using a router function to determine the action to be taken. The router function will then call the appropriate handler function to handle the request.

What is a request handler?

A request handler is a function that handles a request. It is called when a request is received by the server.

What is a route?

A route is a combination of an HTTP method and a path. It is used to match a request to a request handler.

What is a router?

A router is a function that is called before the request handler. It can match a request to a request handler.

What is a request pipeline?

A request pipeline is a chain of functions that are called when a request is received by the server. The request pipeline is composed of a router, a middleware, and a request handler.

What is a middleware?

A middleware is a function that is called before the request handler. It can modify the request and the response.

What is a response?

A response is a message sent by a server to a client. It contains the information about the server and the result of the action the client requested.

🎨 Project Repository

You can find the latest updates of this project on Github

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

Top comments (0)