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.
node.js - Should I make 3rd party API calls in backend or frontend? - Stack Overflow
angularjs - External API calls from the frontend or backend? - Stack Overflow
Question: How does the front-end integrate with the back-end in programming?
Newbie to Express- how to make api calls from frontend JS to backend
Videos
It depends on what your 3rd party API requires.
If you need some credentials to call the API it's probably better to handle the call in backend because of security concerns.
If the API delivers time sensitive data, like some auto-complete information as you type, it might be good to not do the extra roundtrip to the backend and call it from the frontend.
You might create a subdomain which points to the 3rd party server, like 3rdparty-api.yourdomain.com, this removes a lot of cross-domain issues. But this needs cooperation of your 3rd party provider.
So, there is no clear yes or no answer but it depends on the situation and focus of your API.
I would say we should also take care about code duplication. In your case you are all JavaScript, but that is not true for many others. So let's say I consume api.github.com so I will not want to make some calls from frontend and some from the backend, then I think creating a controller which will handle all of this is a good choice.
Except for the cases like any analytics or tracking software, an extra round trip is ok.
As @Wolffc said, this can also prevent sending access_token to the browser which may be misused.
I get that HTML, CSS, and Javascript are front-end languages, but how do we connect the back-end and front-end?
I’ve been using R recently and an R-library called Shiny to help make basic webpages out of my R code. I was wondering how that compares to all these other tools for other languages that all seem related to this front-end/back-end integration.
I've heard React, Django, Angular, Node.js, Vue.js, Express.js, jQuery, etc. get thrown around and I don't really what they are.
In short, what are they? Are they libraries? Programs? How do they compare to Shiny? How do they relate to each other? Thanks!
Bonus questions: PHP vs. HTML/CSS/JS, and what is an API