22
What is an Rest API, the main purpose of API, GET, POST method
API stands for Application Programming Interface. It’s like an engine under the hood. It works behind the scene like a ghost. It helps us to add, delete, update or get all data using this API.
In short, users send a request to API that what do want to do? Then the API responds to the user’s requests.
Let’s explain with an example-
API like a waiter in a restaurant. Someone is waiting to place an order to the waiter from the menu.
Then the waiter goes to the kitchen and tells the chafe about the order. Now chafe the dish according to the order. After making the food waiter will carry the food to the customer.
This whole process like an API works.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Qrtv8PSm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bpilwkd7i5o8qe7xp2t.png)
The main purpose of API is to connect clients with databases. This is like a middleman or connector. Where it helps both sides to connect with each other.
API gets a request from the client or user and collects data from the database as required by the client. Then it will deliver or respond to the client.
Get is a method that helps us to get or fetch all data from the server a client or user requested for.
In short Get method loads data from the server as client requests.
app.get('/order', async (req, res) => {
const cursor = orderCollection.find({});
const result = await cursor.toArray();
res.send(result);
})
Here, all order data loads from the server requested by the client.
This is also related to the server. Using the POST method we can send data to the server from the client-side.
In short, using POST we can send data to a server to create a resource.
app.post('/order/add', async (req, res) => {
const cursor = req.body;
const result = await orderCollection.insertOne(cursor);
res.json(result);
});
In this example, a user sends a new record using the POST method, which will be stored in the database.
22