I have a back end API built with express that is calling OpenAI. I also have a react front end and nginx proxy manager.
PROD set up is react client > cloudflare DNS > NGINX Proxy Manager > Express API which calls OpenAI.
DEV set up is just react client on localhost:3000 and api server on localhost:3001 running on my machine calling openai from localhost:3001 express api
All of a sudden in PROD I have been receiving:
Access to fetch at 'https://api.example.com/upload' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
home.jsx:59
POST https://api.example.com/upload net::ERR_FAILED 504 (Gateway Timeout)
cors is configured on my api server as so:
const allowedOrigins = ['https://example.com'];
app.use(helmet());
app.use(cors({
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
}));
So, I cannot figure out why that CORs error would be thrown.
fetch call from my client:
const response = await fetch(`https://${apiUrl}/upload`, {
method: 'POST',
body: formData,
});
Error on my server side:
Error: Not allowed by CORS
17|leadgen | at origin (file:///root/lead-gen/leadgen-api/indext.js:29:16)
17|leadgen | at /root/lead-gen/leadgen-api/node_modules/cors/lib/index.js:219:13
17|leadgen | at optionsCallback (/root/lead-gen/leadgen-api/node_modules/cors/lib/index.js:199:9)
17|leadgen | at corsMiddleware (/root/lead-gen/leadgen-api/node_modules/cors/lib/index.js:204:7)
17|leadgen | at Layer.handle [as handle_request] (/root/lead-gen/leadgen-api/node_modules/express/lib/router/layer.js:95:5)
17|leadgen | at trim_prefix (/root/lead-gen/leadgen-api/node_modules/express/lib/router/index.js:328:13)
17|leadgen | at /root/lead-gen/leadgen-api/node_modules/express/lib/router/index.js:286:9
17|leadgen | at Function.process_params (/root/lead-gen/leadgen-api/node_modules/express/lib/router/index.js:346:12)
17|leadgen | at next (/root/lead-gen/leadgen-api/node_modules/express/lib/router/index.js:280:10)
17|leadgen | at expressInit (/root/lead-gen/leadgen-api/node_modules/express/lib/middleware/init.js:40:5)
Everything runs fine in my dev setup, and it was previously working in Prod, I made some updates and then began receiving the gateway time out and cors error, however when I rolled back the changes to the previously working version, I am still receiving the errors. so, I do not know what would have changed.
Any insight or help would be much appreciated and please let me know if there are more details I can provide.
Instead of making multiple smaller requests to OpenAI I combined into fewer large requests. I did also update the proxy_timeout in Nginx, but might test what the lowest I can do is for the timeout without running into the timeout error.