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

cors support for route rules #95

Closed
pi0 opened this issue Apr 12, 2022 · 3 comments · Fixed by #538
Closed

cors support for route rules #95

pi0 opened this issue Apr 12, 2022 · 3 comments · Fixed by #538
Assignees
Labels
enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Apr 12, 2022

Context: nuxt/nuxt#13599

Proposal:

routes: {
  '/public': { cors: true }
}

We probably need an h3 utility for this as well: unjs/h3#82

@pi0 pi0 added the enhancement New feature or request label Apr 12, 2022
@pi0 pi0 self-assigned this Apr 12, 2022
@ahku
Copy link

ahku commented Apr 20, 2022

@pi0 What about enabling cors in general? I created another thread under Nuxt and the suggested approach was to create a Nitro plugin. I could however not get it to work the same way it works for express.

This is the code that works with an express server (notice the origin value set to localhost:3100). The client can access the end points just fine:

app.use(
  cors({
    origin: 'http://localhost:3100',
    credentials: true,
  })
)
app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://localhost:3100')
  res.header('Access-Control-Allow-Credentials', true)
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
  res.header(
    'Access-Control-Allow-Headers',
    'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'
  )
  next()
})

The corresponding Nitro plugin does not work. Maybe I messed up somewhere? The client app compains that the origin is set to '*' even though I set it to localhost:

import cors from 'cors'

export default defineNitroPlugin(({ h3App }) => {
  h3App.use(
    cors({
      origin: 'http://localhost:3100',
      credentials: true,
    })
  )
  h3App.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:3100')
    res.header('Access-Control-Allow-Credentials', true)
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    res.header(
      'Access-Control-Allow-Headers',
      'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'
    )
    next()
  })
})

Any help would be appreciated.

@pi0
Copy link
Member Author

pi0 commented Apr 20, 2022

We can globally enable it with routing rules too:

routes: {
  '/**': { cors: true }
}

@gluharry
Copy link

Hey, is there any development on this?
I am also struggling with cors and the options method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants