How does the Internet work?
Published: 2021-08-16 16:54:58

If there is one thing that has revolutionized the world in the 20th century, it is the internet. It has completely changed the way humans communicates and has impacted a lot of lives. Most of us have been a consumer on the internet for the most part of our lives. As web developers we are becoming a creator on the internet. Hence it becomes very essential for us to know how the internet works from a web-developer's point of view. Let's begin!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukuw1dy0h3thicp6fzqj.png)

At a bottom level, what the internet really is your browser or your machine is making requests to a server and is getting responses back. Browser can be on your PC, Mac, phone or any other multimedia device. The server is just another machine with server software running on it. Every device connected to the internet have an IP address assigned to them. In this case both the server and the machine running the browser have an IP address assigned to them. Now when we type an URL(www.xyz.com) on the browser, the browser does not know the server's IP address associated to the URL. Hence it goes to the ISP for the server's address. The ISP does something known as a DNS lookup which is nothing but a search of association of the URL to the server's address. Once the association is found, it returns the IP address of the server to the browser. Now the browser can send direct requests to the server and gather responses.

Now to browser sends connection requests to the server and in return, the server sends a response back to the browser. The response is a file and its content type. The file can be an image file, CSS file or JavaScript file. Usually, the file is index.html and the type is text/html. Now the browser knows what to do with it, so it starts parsing the HTML file provided by the server. During parsing, it usually goes top to bottom and pauses when it finds a request for any asset. An asset is another request that has to be made by the browser to procure that file and its content type. These assets are mostly CSS stylesheets and JavaScript files that are required to ensure the proper functioning of the webpage.

Once the assets are procured from the server, anything on the HTML file that has a reference of the asset files, the assets files are consulted for that. CSS files are referred to add styling to HTML components and JS files are referred to add functionality to HTML components on the webpage. Usually, the CSS files are linked inside the head tag of the HTML files so that the CSS file is procured before parsing components inside the body tag. This step is taken by most web developers to avoid something known as Flash of Unstyled Content (FOUC). Similarly, JavaScript files are sourced at the end inside the body tag because these kinds of files are usually heavy in size and take time to request, procure and parse. Moreover, JavaScript files can request stylesheets and images. This process should not slow down the loading of the webpage because parsing is paused whenever a request for an asset is sent. It is always recommended to club CSS and JS files so that less request is sent by the browser.

Now lets us have a closer look at the requests and responses in which the browser is involved.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovlk5e6vxs2ovh2puklr.png)

The request header consists of few parameters as mentioned in the diagram. Let us discuss each parameter one by one.

1.**Host:** It is nothing but the request URL to which the browser wants to connect.
1.**Request Method:** It can be anything from GET, POST, PUT or DELETE. Request using GET should only retrieve data from the server. The POST method is used to submit entities to specified resources on a server. The PUT method replaces all current representations of the target resource with the request payload. The DELETE method deletes the specified resource.
1. **Path:** It is the path of the resource to which the browser is requesting to access or procure.
1. **Cookies:** It is a string that the browser stores to improve the users experience on the website. It is been sent to the server in a hope that the response will be curated as per the user.
1. **User String:** It is a string that contains information about your web browser and the device you are using.
1. **Content Type:** It is the type of content that that the browser is expecting the server to give.
1. **Payload:** When we use the POST method we send data to the server in JSON or XML format. It is known as the payload with your request.

The response consist of a header and a response body. The header contains the response information and the body contains the resource from the server.

1. **Content Type:** It is the information about the content type of the resource sent by the server. Using this the browser parses the resource file.
1. **Status Code:** It is a message from the server in form of a code. Code 200 means Successful response, 404 means not found, 500 means internal server error. These were a few common codes.
3. **Response Body:** The response body consist of a file that the browser requested or a result that the server has sent to the browser. The file can be a HTML file, CSS file, a JS file or an image file. Results are mostly in form of JSON or XML format.