CORS error in Netlify functions (Redirect)

Hello, I have created an express app hosted on a netlify function (and deployed with netlify-lambda command).
When I try to POST something to one these routes, if I use Postman the API is working, if it’s from my website, I get
Access to XMLHttpRequest at 'https://****' from origin '*****' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

I have no idea why it is talking about redirect as I have no redirects set in my API server.
I hope someone can help me on this.
I am posting below the basic code of the express server, in case I missed something.

const basePath = process.env.APP_ENV === 'local' ? '' : '/.netlify/functions/api'

const express = require('express')
const serverless = require('serverless-http')
const bodyParser = require('body-parser')
const cors = require('cors')
const helmet = require('helmet')
const compression = require('compression')

const likes = require('./routes/likes')

const app = express()

app.get(`${basePath}/`, (req, res) => {

app.use(`${basePath}/likes`, likes)

module.exports = app
module.exports.handler = serverless(app)

Just solved this, by realizing recently Netlify updated the URL of projects with no custom domains.
I was accessing my old URL which Netlify was redirecting to the new .app URL, hence the CORS error about redirect…

Hope it helps, if someone like me forgot to update their code during the transition.

1 Like