Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add cors settings (origin)? #2903

Open
1 task
MickL opened this issue Nov 25, 2024 · 2 comments
Open
1 task

How to add cors settings (origin)? #2903

MickL opened this issue Nov 25, 2024 · 2 comments

Comments

@MickL
Copy link
Contributor

MickL commented Nov 25, 2024

Describe the feature

At the moment I have the following rule in my nitro.config.ts:

routeRules: {
    '/**': {
      cors: true,
    },
  },

I want to only accept cors for my website-domain (prod and dev). How can I make options like cors origin in Nitro? Unfortunately the whole cors topic is not covered in the docs.

E.g. my domain is www.example.com and my api is at api.example.com -> Without cors: true this results in errors.

Reference

Additional information

  • Would you be willing to help implement this feature?
@Shooteger
Copy link

This should help, #539

Official docs for routeRules shows how to handle special routes in dev or prod individually: https://nitro.build/config#routerules

You could use devProxy, if I understand correctly what you want to achieve on localhost:

{
  devProxy: {
    '/proxy/test': 'http://localhost:3001',
    '/proxy/example': { target: 'https://example.com', changeOrigin: true }
  }

@MickL
Copy link
Contributor Author

MickL commented Nov 25, 2024

I have no problem making a differntiation between dev and prod. I want to set cors origin to a specific URL instead of accepting all origins.

For example in Express cors has an options attribute where you can set origin and other things. You could even use an async function for this:

var corsOptions = {
  origin: function (origin, callback) {
     db.loadOrigins(function (error, origins) {
      callback(error, origins)
    })
  }
}

app.use(cors(corsOptions));

What I am looking for is something like this:

routeRules: {
    '/**': {
      cors: {
           origin: 'www.example.com'
      },
    },
  },

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

No branches or pull requests

2 participants