I'm working in javascript(React) to create a web app that makes use of multiple APIs(Spotify, Twitch, Youtube) and so far I have been using Axios to make the REST calls successfully. But now I have begun running into Cross-Origin Resource Sharing (CORS) errors and I have been told that I need to make calls to external APIs from a server instead of from my client. I've never made API calls from a server and have some questions:
- Everything I am doing is currently running locally using Node and I dont have a "server" unless that's what Node counts as. Do I need to get a "server"?
- Should I be creating my own API and hosting it on some server so that I can call that API from my javascript code?
- How do I create my own API if that's what I should be doing?
- Is there a different language I will need to use to make server-side api calls?
I'm working in javascript(React) to create a web app that makes use of multiple APIs(Spotify, Twitch, Youtube) and so far I have been using Axios to make the REST calls successfully. But now I have begun running into Cross-Origin Resource Sharing (CORS) errors and I have been told that I need to make calls to external APIs from a server instead of from my client. I've never made API calls from a server and have some questions:
- Everything I am doing is currently running locally using Node and I dont have a "server" unless that's what Node counts as. Do I need to get a "server"?
- Should I be creating my own API and hosting it on some server so that I can call that API from my javascript code?
- How do I create my own API if that's what I should be doing?
- Is there a different language I will need to use to make server-side api calls?
- CORS is server side error. your backend developer need to slove it and after that you can make API call. – Irin Commented Jun 24, 2019 at 16:14
- @Irin — CORS is not a server-side error, it is a client-side error caused by making a request to an origin which doesn't grant permission to access it. The OP said they were making request to Spotify, Twitch, and YouTube APIs … their backend developer (who may not even exist) won't be able to change those APIs (which brings us back to the question the OP actually asked). – Quentin Commented Jun 24, 2019 at 16:16
- @BigGerman — It's React. They can't be running it from the filesystem, it just won't work from there. – Quentin Commented Jun 24, 2019 at 16:16
2 Answers
Reset to default 2Everything I am doing is currently running locally using Node and I dont have a "server" unless that's what Node counts as. Do I need to get a "server"?
React es with a bunch of development tools that use Node, including a development server. It isn't designed for production use though, so you shouldn't use it for that.
Should I be creating my own API and hosting it on some server so that I can call that API from my javascript code?
Yes.
How do I create my own API if that's what I should be doing?
Write some code which accepts an HTTP request, gets the data you want to respond to it with, and makes an HTTP response.
Express.js is a popular way to do this in Node. You can bine it with Next.js to apply server-side rendering for your React app (resulting in better performance, accessibility, reliability, and SEO).
Is there a different language I will need to use to make server-side api calls?
You can write your server-side code in any language you like.
- I assume you are hosting your application on a development nodeJS server, therefor you will need an extra server.
- Yes. Create an API and call it from your frontend.
- Create a server which takes http requests and does your stuff according to the route chosen.There are many examples on how to do this with for example nodeJS+Express on the internet.
- The Language you use for the server side is your choice.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745650803a4638253.html
评论列表(0条)