Contenido del Curso
Node.js Express: API & CLI Apps
Node.js Express: API & CLI Apps
HTTP Requests
Before we dive deeper into developing web applications, let's take a moment to revisit some crucial theory that underpins our understanding of what we're about to build and why.
🔄 Understanding the HTTP Request-Response
HTTP (Hypertext Transfer Protocol) is the foundation of data communication between a client (typically a web browser or an application) and a server. This protocol enables clients to request and receive server resources.
Request-Response Model: HTTP operates on a request-response model. Imagine a client sending an HTTP request to a server, and in return, the server promptly furnishes an HTTP response containing the requested data.
🏛️ Real-World Analogy (Visiting a Library)
To make this concept relatable, let's draw an analogy with a real-world scenario: visiting a library.
Client-Server Communication
As the client, you are in one room (the library), while the librarian at the front desk is in another room (the server). Just as in web communication, you can't directly access the books (resources); you need to communicate with the librarian (server) to get what you want.
Request-Response Model
Requesting a book isn't as simple as picking it off the shelf; there's a structured process. You approach the librarian and ask for a specific book - this mirrors an HTTP request.
Server Response
The librarian (server) takes your request seriously. They search the shelves (the server processes your request), locate the book, and hand it over to you. This action mirrors the server sending you an HTTP response containing the requested data (the book).
Communication Flow
The interaction between you and the librarian continues as long as you need more books. With each book you request, the librarian retrieves it and hands it to you. Similarly, in HTTP, the client can make multiple requests, and the server responds to each one.
Note
In the upcoming chapters, we will learn how to become the librarians of the web world, handling requests and serving resources.
📤 Types of HTTP Requests
HTTP defines several request methods, each with a specific purpose:
- GET: Use it to retrieve data from a specified resource. It shouldn't have any side effects on the server. For example, fetching a webpage, an image, or data from an API;
- POST: Use it to submit data to be processed at a specified resource. It can create a new resource or update an existing one;
- PUT: Employ it to update a current resource with new data. Unlike POST, which can create new resources, PUT is idempotent, meaning the same operation can be repeated without changing the result;
- DELETE: Request the removal of a resource. For instance, it's like deleting a user account or a file;
- PATCH: Use it to apply partial modifications to a resource. It's often used for updating specific fields of an existing resource.
¡Gracias por tus comentarios!