Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
Express CORS middleware to handle both preflight and normal requests

I have CORS globally configured like this, to handle credentials:

app.use(cors({ origin: 'https://example.com', credentials: true }))

But on certain routes, I need to allow options requests, so following the documentation, I'm doing something like this:

router.options('/someroute', cors())
router.get('/someroute', cors(), someOtherMiddleware, async (req, res) => {
  // do stuff
}

My global CORS policy seems to override the route policy, but only on the options method, I think. I'm trying to come up with a way to make both of these CORS policies play nicely. I have also tried the following, but this doesn't work either. Whichever one I put first seems to override the other one.

app.options('*', cors())
app.use(cors({ origin: 'https://example.com', credentials: true }))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The reason my 2nd code snippet above wasn't working as expected was because I needed to add the preflightContinue property when setting the cors policy at the app level. Otherwise it will ignore cors policies on options routes set at the route level. Kind of a poorly documented thing.

The final solution was to do this at the app level:

app.use(cors({ 
  origin: 'https://example.com', 
  credentials: true,
  preflightContinue: true,
}))

Now this cors policy on the options route actually works:

router.options('/someroute', cors())
router.get('/someroute', cors(), someOtherMiddleware, async (req, res) => {
  // do stuff
}

Ugh, spent so long figuring this one out. Hope this saves someone some time.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda