Search code examples
javascriptrestserver-side

How to make server-side API requests?


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?

Solution

  • 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 comes 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 combine 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.