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

Does fastify contain cors ? #326

Closed
wuchuwei opened this issue Oct 2, 2017 · 14 comments
Closed

Does fastify contain cors ? #326

wuchuwei opened this issue Oct 2, 2017 · 14 comments
Labels
plugin suggestion Ideas for new plugins

Comments

@wuchuwei
Copy link

wuchuwei commented Oct 2, 2017

Does fastify contain cors
thx

@StarpTech
Copy link
Member

Good question. We should definitely support this as a plugin. @delvedor WDYT.

@StarpTech
Copy link
Member

StarpTech commented Oct 2, 2017

We could take this as a roadmap.

@delvedor
Copy link
Member

delvedor commented Oct 2, 2017

It is not supported in core, but it could be a perfect plugin!

@delvedor delvedor added the plugin suggestion Ideas for new plugins label Oct 2, 2017
@mcollina
Copy link
Member

mcollina commented Oct 2, 2017

you can use the http://npm.im/cors module through our middleware support.

@StarpTech
Copy link
Member

StarpTech commented Oct 2, 2017

Example

const cors = require('cors')
fastify.use(cors())

@mcollina but we don't provide a way to attach specific middlewares to a route correct?

@mcollina
Copy link
Member

mcollina commented Oct 2, 2017

@StarpTech it should be fastify.use(cors()).

@gj
Copy link
Contributor

gj commented Oct 18, 2017

@mcollina but we don't provide a way to attach specific middlewares to a route correct?

This looks like it does the trick: https://github.com/fastify/middie#restrict-middleware-execution-to-a-certain-paths

@mcollina
Copy link
Member

closing as it should be solved.

@zhaoyao91
Copy link
Contributor

zhaoyao91 commented Aug 8, 2018

won't CORS be implemented as a plugin?

current cors solution fails to handle so-called preflight request cors problem. see

In preflight request situation, the client will first send an options request to the server and generally, we only define the urls and methods we care about and leave the cors details to the middleware or plugins. This will lead to the options request failing since it is not defined in your app.

I am not sure about the detail, but using express didn't expose such a problem.

@mcollina
Copy link
Member

mcollina commented Aug 8, 2018

I think we should do a plugin for cors.

If you look at: https://github.com/expressjs/cors#enabling-cors-pre-flight, you can see how they define the options route for a request.

IMHO our plugin should define fastify.enableCors('*') (or a specific path) that goes and creates that options route.

Side note, the following might just work together with the cors widdleware:

fastify.route({
  method: 'OPTIONS'
  path: '*'
}, (request, reply) => { reply.send() })

@zhaoyao91 let me know if that works for you.

@mcollina mcollina reopened this Aug 8, 2018
@OhIAmFine
Copy link

OhIAmFine commented Aug 9, 2018

Thanks for your advices, we made it successfully. @mcollina @zhaoyao91

In our practice, when we send a preflighted request by XMLHttpRequest in client, it will first send an options request, so we make it successfully by using cors and fastify.options in our plugins .

 fastify.use(cors())
 fastify.options('*', (request, reply) => { reply.send() })

@delvedor
Copy link
Member

I'm working on a plugin :)

This was referenced Aug 11, 2018
@delvedor
Copy link
Member

Closed in #1076 :)

delvedor added a commit that referenced this issue Aug 15, 2018
As titled.

Related: #326

#### Checklist

- [ ] run `npm run test` and `npm run benchmark`
- [ ] tests and/or benchmarks are included
- [x] documentation is changed or added
- [x] commit message and code follows [Code of conduct](https://github.com/fastify/fastify/blob/master/CODE_OF_CONDUCT.md)
@SarasArya
Copy link

Thanks for your advices, we made it successfully. @mcollina @zhaoyao91

In our practice, when we send a preflighted request by XMLHttpRequest in client, it will first send an options request, so we make it successfully by using cors and fastify.options in our plugins .

 fastify.use(cors())
 fastify.options('*', (request, reply) => { reply.send() })

But you have write it in every file if you are using prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin suggestion Ideas for new plugins
Projects
None yet
Development

No branches or pull requests

8 participants