I am trying to create an application with supabase as DB, vercel backend (url: http://localhost:3000/api/) and netlify frontend (url: http://localhost:5173). The frontend is a react application with vite and the backend is a list of serverless functions with express.
The problem is when I try to make a request from the frontend I get the 500 error CORS Preflight Did Not Succeed. That happens when I add the CORS headers to vercel according to documentation.
I have also tried to use the cors middleware from npm and add the headers the express way but for some reason the headers are not send (I also don't seem to be able to change any header this way).
Any help would be greatly appreciated.
vercel.json:
{
"headers": [
{
"source": "/api/(.*)",
"headers": [
{ "key": "Access-Control-Allow-Credentials", "value": "true" },
{ "key": "Access-Control-Allow-Origin", "value": "http://localhost:5173" },
{ "key": "Access-Control-Allow-Methods", "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
{ "key": "Access-Control-Allow-Headers", "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }
]
}
]
}
This is the create app function which I use to initialize all the functions so I don't write the same parameters for all the functions.
import express, { NextFunction, Request, Response, json } from "express"
import helmet from "helmet"
import cors from "cors"
import cookieParser from "cookie-parser"
export function createApp() {
const app = express()
app.use(customCorsMiddleware)
app.use(cookieParser())
app.use(helmet())
app.use(json())
const url = process.env.CORS_URL
const middle = cors({ origin: true, credentials: true,})
app.use(middle)
return app
}