Skip to content

Conversation

vimutti77
Copy link

@vimutti77 vimutti77 commented Aug 20, 2025

Checklist

This PR adds support for configuring CORS on a per-route basis via config.cors. This allows developers to override or disable CORS behavior for specific routes, while still using the global plugin configuration as a default.

const fastify = require('fastify')()

fastify.register(require('@fastify/cors'), { origin: 'https://example.com' })

fastify.get('/cors-enabled', (_req, reply) => {
  reply.send('CORS headers applied')
})

fastify.get('/cors-allow-all', {
  config: {
    cors: {
      origin: '*', // Allow all origins for this route
    },
  },
}, (_req, reply) => {
  reply.send('Custom CORS headers applied')
})

fastify.get('/cors-disabled', {
  config: {
    cors: false, // Disable CORS for this route
  },
}, (_req, reply) => {
  reply.send('No CORS headers')
})

fastify.listen({ port: 3000 })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant