3
votes

I have a node/express api deployed (api.mysite.com)

In the express app, I have used app.use(cors());

In the frontend I have a nuxt/vue site (www.mysite.com).

The vue site uses the api to fetch some data. The problem is that most times it works fine. But some times (2 out of 10), I get the following error:

Failed to load resource: the server responded with a status of 504 (Gateway Time-out)

Access to XMLHttpRequest at 'api.mysite.com' from origin 'www.mysite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've read a lot but cannot figure out why it's happening specially why only sometimes and not the other times?

1
What did you use to fetch date in the front-end side?Vu Luu
@vuluu In the front-end, I use axios to call the api.asanas
And it's a post requestasanas
I think the cors error is a red herring. It looks like your api is (sometimes) taking too long to respond which could be for lots of reasons. Try either making axios have no timeout, or try debugging requests to your api.Countingstuff
Where are you hosting the server?. You're behind a load balancer and it's ending the request due to the server not responding.Marcos Casagrande

1 Answers

3
votes

Set the timeout to a higher value. If your request is taking more time to be served than the current timeout it throws Gateway Timeout

var server= http.createServer(app).listen(port, function()
{
    console.log("Listening on port " + port)
})
server.timeout = 240000;