What is an API?
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.
Purpose of API
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.
Uses of Get
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.
Uses of Post
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.
Top comments (0)
Some comments have been hidden by the post's author - find out more