Find the affected request in the browser
A static site at https://site.example.com and an API at https://api.example.com have different origins. A request that succeeds in a terminal may still require CORS response headers for JavaScript in the browser to read it.
Open the Network panel on the published site. Check which API URL it calls, its Origin header and whether the browser sends OPTIONS before the application request. If the API is unreachable or still serving an old route, fix that first. For a Vite build, changing the public API address requires rebuilding and publishing the frontend bundle. Publish the current Vite build.
Allow the frontend origin on the API
Install the Express cors package and deploy the updated npm manifest and lockfile. In your existing server, add the middleware for /api before the API handlers. This example allows an unauthenticated API’s GET and JSON POST requests from one HTTPS site. Substitute your site’s exact origin, with no path or trailing slash.
import cors from 'cors';
app.use('/api', cors({
origin: ['https://site.example.com'],
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type'],
}));Keep your API’s authentication and authorization in place for private data. CORS controls whether a browser can expose the response to a page. For a UI and API in one Node.js app, a relative request such as fetch('/api/health') can use the same origin. Express CORS middleware.
Verify the preflight and the real request
Set API_URL and SITE_ORIGIN to the actual HTTPS origins. Replace /api/preview with an existing JSON POST route that the page calls.
curl -sS -i -X OPTIONS "$API_URL/api/preview" \
-H "Origin: $SITE_ORIGIN" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type"Look for a successful response with your site’s origin in Access-Control-Allow-Origin, POST in the allowed methods and content-type in the allowed headers. Reload the published frontend and check that its POST runs and returns the expected JSON. If both services run in one deployment, see serving React and Express on one origin.