CORS configuration ineffective.

default discord avatar
clhome
last month
2 1

When I configured cors: '*' in payload.config.ts, I still got a CORS error when accessing it with other ports. Can anyone please advise on how to solve this issue?

  cors: '*', 
  rateLimit: {
    trustProxy: true,
    window: 2 * 60 * 1000, // 2 minutes
    max: 2400, // limit each IP per windowMs
  },

image-20230404223530906

  • default discord avatar
    clhome
    last month

    I have found a solution to fix the CORS issue, but it's not elegant. I believe the server.ts configuration not working should be considered a bug.The code is written in server.ts.

    var cors = require('cors');
    var corsOptions = {
      origin: '*',
      credentials: true,
      optionsSuccessStatus: 200
    }
    
    app.use(cors(corsOptions));

    it need run“yarn add cors” first,

    3 replies
  • discord user avatar
    JarrodMFlesch
    Payload Team
    last month

    Normally, you set cors to an array of domains that you would like to accept requests from. Setting cors to * is generally not recommended, you should instead add the domains to the cors array like so:

    cors: ['http://localhost:3000']
  • default discord avatar
    clhome
    4 weeks ago

    I try to write like cors: ['http://localhost:3000'] before in payload.config.ts,but it`s not work. "*"just for test

  • default discord avatar
    8blvck
    3 weeks ago

    Normally, you set cors to an array of domains that you would like to accept requests from. Setting cors to * is generally not recommended, you should instead add the domains to the cors array like so:

    cors: ['http://localhost:3000']

    http:// is not a part of domain, and cors: '*' not working.

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.