Doing an API request in the frontend or backend? What's best practices?
node.js - How to call external api's data from backend to frontend side? - Stack Overflow
What do you use to quickly create a frontend for your api?
How do I connect backend to frontend? I'm having issues with CORS
Videos
I was wondering because someone told me not to expose your keys in the front end even if you store it in an env file. But if you do the api request in the backend you have to do another axios request to get it in the front end. So anyone know what's the right or best method? 2 request for more security vs just 1?
I would recommend to use a templating engine like handlebars or ejs.There are tons of examples for it, and sending data from backend to frontend becomes a piece of cake when using any templating engine. my personal favourite is handlebars because of its simple syntax.
It is advisable not to use document.querySelector if you're using Angular or React. React/Angular will have the browser repaint the DOM by making updates in the "root" div element of the index.html file whenever there is new data available to update. Also, why do you want to send a HTML file? You could have a route in Node like below
route.get('/weather', (req, res) => {
// do your api call with axios to get weather data
res.json(weatherData);
});
from your front-end you could make an API call to '/weather' route and consume the JSON data
axios.get('baseUrl/weather').then(res=>{
console.log("weather data", res);
}).catch(...);
You could also fetch weather data directly from front-end like above.