Expressjs Server cannot handle Requests from the Outside

Viewed 411

I have a ExpressJs Server with React Components. And the Server should handle Requests from Outside and one request should play a Song from the Spotify API when not currently playing.

app.post("/play", (req, res) => {
  try {
    // requesting to play uses query params
    id = req.query.id;
    currPlayingID = 0;

    // get the currently playing song from the SPotify API
    axios({
      url: "https://api.spotify.com/v1/me/player/currently-playing",
      method: "get",
      headers: {
        authorization: `Bearer ${access_token}`,
      },
    })
      // set the currently Playing ID or to zero if nothing is playing
      .then((response) => {
        if (response.data !== null) {
          currPlayingID = response.data.id;
        } else {
          currPlayingID = 0;
        }
      });

    // only play the song if its not currently playing
    if (id !== currPlayingID) {
      // making a axios request to the Spotify API to play the Song with the ID
      axios({
        url: "https://api.spotify.com/v1/me/player/play/",
        method: "put",
        headers: {
          authorization: `Bearer ${access_token}`,
        },
        data: {
          uris: [`spotify:track:${id}`],
        },
      });
      res.status(204);
    }
  } catch (error) {
    res
      .status(404)
      .json({ message: "Couldn't get Info from Spotify API", error: error });
  }
});

The Problem:

The Code works when I start the server on the device itself (so a local server on my Desktop PC), but when I start the Server on my RaspberryPI i cannot handle Requests to this endpoint /play. Yeah I updated all the IP Adresses, everywhere.

But the moer ointeresting part is using the React Client I get this error:

Failed to load resource: net::ERR_CONNECTION_REFUSED

Requesting with POSTMAN I get the following:

Mixed Content Error: The request has been blocked because it requested an insecure HTTP resource

And from a request using a python script I get on the server side:

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 400".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I have no clue how to fix each error and if it is one fix. Basically I found out it is a Problem with rejeccting requests from outside localhost, because with cURL on my ssh terminal it works.

2 Answers

I'm learning express, so I m not an expert, but I'm looking at your errors. I will suggest you try asyncHandler module. It handles asynchronous requests and exceptions.

I faced a similar issue because while I'm sending the API request via Axios, my token is null/empty/wrong, so make sure your token is correct

this is my request format

        axios({
               method:"POST",
               url:"https://graph.facebook.com/v13.0/"+phon_no_id+"/message?access_token="+token,
               data:{
                   messaging_product:"whatsapp",
                   to:from,
                   text:{
                       body:"Hi.. I'm Prasath"
                   }
               },
               headers:{
                   "Content-Type":"application/json"
               }

           });
Related