admin管理员组

文章数量:1435859

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:

  1. 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"?
  2. Should I be creating my own API and hosting it on some server so that I can call that API from my javascript code?
  3. How do I create my own API if that's what I should be doing?
  4. 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:

  1. 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"?
  2. Should I be creating my own API and hosting it on some server so that I can call that API from my javascript code?
  3. How do I create my own API if that's what I should be doing?
  4. Is there a different language I will need to use to make server-side api calls?
Share Improve this question asked Jun 24, 2019 at 16:09 Isaac PerezIsaac Perez 5904 gold badges17 silver badges33 bronze badges 3
  • 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
Add a ment  | 

2 Answers 2

Reset to default 2

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"?

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.

  1. I assume you are hosting your application on a development nodeJS server, therefor you will need an extra server.
  2. Yes. Create an API and call it from your frontend.
  3. 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.
  4. The Language you use for the server side is your choice.

本文标签: javascriptHow to make serverside API requestsStack Overflow