Currently, I am setting up a server through express.
While everything is running smoothly with my project, I have a small query that is not related to the project itself.
My confusion lies in the requirement to use the GET method when, in my opinion, using POST would be more logical.
Basically, I am configuring an API key on the server side and then retrieving it on the client side for usage.
Here is a snippet of the server-side code:
const apiKey = process.env.API_KEY;
console.log(`Your API key is ${apiKey}`);
const dataObject ={};
app.get('/api', (req,res) => {
res.send({key: apiKey})
})
app.get('/all', sendData = (req,res) => {
res.send(dataObject)
})
app.post('/addText', (req,res) => {
let newEntry = {
agreement = req.body.agreement,
subjectivity = req.body.subjectivity
}
dataObject = newEntry;
res.send(dataObject);
} )
On the client side, I retrieve from the '/api' path:
const getApiKey = async () => {
// Getting API key from server
const request = await fetch('/api');
try {
const data = await request.json();
console.log(data);
return data;
}catch(error) {
console.log('ERROR', error);
}
}
Everything is functioning correctly, but I have a question:
- With the initial GET request on the server side, I am aware that I am sending the API key to the '/api' path in order to retrieve it on the client side with
fetch
. However, if I am sending the API key to this path, why am I utilizing GET instead of POST?
I apologize if this question seems trivial, but I am struggling to grasp the concept of the GET method.
Thank you!