What is HTTP The Hypertext Transfer Protocol (HTTP) is the foundation of the World Wide Web, and is used to load webpages using hypertext links. HTTP is an application layer protocol designed to transfer information between networked devices and runs on top of other layers of the network protocol stack. A typical flow over HTTP involves a client machine making a request to a server, which then sends a response message.
What is an HTTP METHOD: An HTTP method, sometimes referred to as an HTTP verb, indicates the action that the HTTP request expects from the queried server. The most common HTTP methods are Get, Post, Put, Patch, Delete.
GET
The GET method is used to retrieve data from a server. It is one of the most commonly used HTTP methods.
Here's an explanation of how the GET method works in Node.js:
- The client (usually a web browser) sends a GET request to the server.
- The server receives the request and processes it.
- The server retrieves the requested data and sends it back to the client in the response body.
- The client receives the response and displays the data to the user.
POST
The POST method is used to send data to a server to create or update a resource. Here's an explanation of how the POST method works in Node.js:
How POST Requests Work
- The client (usually a web browser or a mobile app) sends a POST request to the server.
- The request body contains the data to be sent to the server.
- The server receives the request and processes it.
- The server creates or updates a resource based on the data sent in the request body.
- The server sends a response back to the client, usually with a status code of 201 Created or 200 OK.
PUT
The PUT method is used to update an existing resource on the server.
Here's an explanation of how the PUT method works in Node.js:
How PUT Requests Work
- The client (usually a web browser or a mobile app) sends a PUT request to the server.
- The request body contains the updated data for the resource.
- The server receives the request and processes it.
- The server updates the existing resource with the new data.
- The server sends a response back to the client, usually with a status code of 200 OK or 204 No Content.
PATCH
The PATCH method is used to partially update an existing resource on the server. It is similar to the PUT method, but instead of replacing the entire resource, it only updates the specified fields.
How PATCH Requests Work
- The client (usually a web browser or a mobile app) sends a PATCH request to the server.
- The request body contains the updated fields for the resource.
- The server receives the request and processes it.
- The server updates the existing resource with the new data.
- The server sends a response back to the client, usually with a status code of 200 OK or 204 No Content.
DELETE
The DELETE method is used to delete a resource from the server. Here's an explanation of how the DELETE method works in Node.js:
How DELETE Requests Work
- The client (usually a web browser or a mobile app) sends a DELETE request to the server.
- The request URL specifies the resource to be deleted.
- The server receives the request and processes it.
- The server deletes the specified resource.
- The server sends a response back to the client, usually with a status code of 200 OK or 204 No Content.
What’s an HTTP status code?
HTTP status codes are 3-digit codes most often used to indicate whether an HTTP request has been successfully completed. Status codes are broken into the following 5 blocks:
1xx Informational
2xx Success
3xx Redirection
4xx Client Error
5xx Server Error
The “xx” refers to different numbers between 00 and 99.
Status codes starting with the number ‘2’ indicate a success. For example, after a client requests a webpage, the most commonly seen responses have a status code of ‘200 OK’, indicating that the request was properly completed.
If the response starts with a ‘4’ or a ‘5’ that means there was an error and the webpage will not be displayed. A status code that begins with a ‘4’ indicates a client-side error (it is very common to encounter a ‘404 NOT FOUND’ status code when making a typo in a URL). A status code beginning in ‘5’ means something went wrong on the server side. Status codes can also begin with a ‘1’ or a ‘3’, which indicate an informational response and a redirect, respectively.
Top comments (0)