Intro to servers, their relationship with web browsers, and the role they play in the wider context of web development.
At the heart of the internet, we find two key players: servers and browsers.
Any computer can act as a server. There’s nothing inherently special about a server. In fact, your personal laptop could function as one. However, in practice, servers tend to be more powerful machines due to the demanding tasks they often perform. They are computers configured to run software that understands HTTP requests and provides data and services to other computers, known as clients, over a network.
The server is the second half of the HTTP model. The client sends requests for resources, and the server receives these requests and sends responses. Here’s a simple example:
GET /search?q=cats HTTP/1.1 accept: text/html
HTTP/1.1 200 Ok content-type: text/html <!doctype html> <html> <body> <img src="funny-cat.jpg" alt="A very fat cat"> </body> </html>
In this example, the client is requesting a search for “cats,” and the server responds with an HTML document containing an image of a cat.
A web browser is a software application that executes untrusted code sent over the internet. To ensure security, browsers have strict limits on what they can do. This limitation is often referred to as the “sandbox.” For example, they can’t read files on your Desktop. As a web developer, you have no control over the browser environment - users can run any browser software, apply custom CSS, use extensions, or block specific parts of the page.
The key difference between browsers and servers lies in the control and execution of code. In a browser, your code is running on their computer (i.e. the user’s computer). In contrast, on a server, your code is running on your computer.
- Servers have no DOM: The Document Object Model (DOM) doesn’t exist until a browser receives an HTML response and constructs it. This means
documentobjects are undefined on the server.
- Servers are Secret: In the browser, all code is sent to the user, and they can view it all in the developer tools. Users cannot see the code running on your server (assuming it is secured correctly). They will only see what you send in the HTTP response. This means it’s safe to use API keys and other secrets on your server.
- Servers are Shared: In the browser, your code runs separately on each user’s computer, meaning one user can’t see another user’s data. You usually have one server for all users, which means you can share variables between requests. However, be careful, as you could accidentally show one user’s data to another.
- Ways to access the underlying computer that browsers don’t have.
While browsers implement Web APIs to provide useful features like
element.clientWidth, Node doesn’t implement these since they mostly don’t make sense on a server. Instead, Node provides additional APIs for tasks like creating an HTTP server and accessing the computer’s filesystem.