API stops working on Express server side randomly

This is my server code:

import express from 'express';
import cors from 'cors';
import axios from 'axios';

const app = express()
const port = 3000;
app.use(cors());

app.get('/', async(req, res) => {
    await axios.get("<functional URL confirmed>")
        .then(response => {
            console.log(response.data);
        })
})

app.listen(port, () => {
    console.log(`App listening at http://localhost:${port}`);
});

The above code sometimes works perfectly and logs the data as expected. Most other times however, it outputs a very long error that looks something like this:

Error Screenshots:

enter image description here

enter image description here

Changed absolutely NOTHING in my code, just re-ran after a minute or so, and:

enter image description here

What I have already tried to solve this:

  • Checked my internet connection and speed. Everything is a-ok.
  • Checked my API quota. I am not violating it. I know because I can npm run dev my frontend which uses that api with the same url and output the same data with no errors even while I see this error on the backend
  • Checked the URL. I know it’s correct because of the same reason as above.
  • Tried waiting for a minute before re-running the same code. It worked. Then I ran it again. Didn’t work.
  • Assumed it was a time limit thing. Wrong. Waited for 15 minutes before re-running, still didn’t work.
  • Cut and pasted the same URL again just to change something. Data logged successfully.
  • Re-ran the code. Error again.

I don’t know what’s causing this erratic behaviour.

In case anyone wonders why I’m even using an Express server when the API can be called directly in my React frontend, it’s because this preliminary call isn’t causing issues but some other stuff on the same URL is giving me CORS errors. However the url I’m using here to test my server-side code is the same one that is definitely functional in the frontend.

Can someone please explain what is going on behind the scenes?