Comments (16)
Apologies for the slow reply. What we think might be happening, is that in our application, we are registering a main app.js plugin, after index.js. https://github.com/58bits/fastify-20/blob/master/index.js - and that if instead the fastify-cors module is loaded in index.js (top level scope?) it works.
We still don't know 100% why, and have not had time to dig into find-my-way, or the problem in more detail.
For now - we've simply created our own plugin, using the '/*'
catch-all route as follows (leaning heavily on the fastify-cors module as an example).
'use strict'
const fp = require('fastify-plugin')
async function setup (app) {
// Wildcard OPTIONS handler for CORS preflight requests
app.route({
method: 'OPTIONS',
url: '/*',
handler: async (request, reply) => {
var reqAllowedHeaders = request.headers['access-control-request-headers']
if (reqAllowedHeaders !== undefined) {
reply.header('Access-Control-Allow-Headers', reqAllowedHeaders)
}
reply.code(204)
.header('Content-Length', '0')
.header('Access-Control-Allow-Origin', 'http://localhost:8080')
.header('Access-Control-Allow-Credentials', true)
.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE')
.send()
}
})
// CORS reply - 'Access-Control-Allow-Origin', '*' for now..
// See https://github.com/fastify/fastify-cors/issues/20
app.addHook('onRequest', function (request, reply, next) {
reply.header('Access-Control-Allow-Origin', 'http://localhost:8080')
reply.header('Access-Control-Allow-Credentials', true)
next()
})
}
module.exports = fp(setup)
from fastify-cors.
Try this... https://github.com/58bits/fastify-20
from fastify-cors.
Sorry both @daveamayombo and I are on the road until later this week. Stay tuned...
from fastify-cors.
This is a different problem as OP says OPTIONS, but I struggled to fix curl -I http://127.0.0.1:3000
returns 404
.
For others who miss the document, this helped me a lot.
const app = fastify({
exposeHeadRoutes: true, // <-- this option
})
Related
from fastify-cors.
I think I've found the problem. Implementing my own wildcard route for OPTIONS requests using /*
instead of *
works.
e.g.:
.route({
method: 'OPTIONS',
url: '/*',
handler: async (request, reply) => {
reply.code(204)
.header('Content-Length', '0')
.header('Access-Control-Allow-Origin', '*')
.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE')
.send()
}
})
Is this possibly related to fastify/fastify#81 ?
I'm using fastify v2.0.1 and fastify-cors v2.1.2 on node v10.15.3
from fastify-cors.
Can you please add a full repro for your problem? Something I can execute straight away.
It looks like it might be a bug somewhere cc @delvedor
from fastify-cors.
Sure - I'll setup a public repo in a little while.
from fastify-cors.
@delvedor can you take a look? This seems something that goes deep down in find-my-way.
from fastify-cors.
I have the same issue. How to solve this?
from fastify-cors.
I think I've found the problem. Implementing my own wildcard route for OPTIONS requests using
/*
instead of*
works.
e.g.:.route({ method: 'OPTIONS', url: '/*', handler: async (request, reply) => { reply.code(204) .header('Content-Length', '0') .header('Access-Control-Allow-Origin', '*') .header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE') .send() } })Is this possibly related to fastify/fastify#81 ?
I'm using fastify v2.0.1 and fastify-cors v2.1.2 on node v10.15.3
I have tried your workaround. But not work for me
from fastify-cors.
Quick note with more to come - but it may be that the way we are registering app.js - without using fastify-plugin, is affecting the scope of fastify-cors.
from fastify-cors.
it seems you folks a have a better understanding of your issue than us. Would you like to send a PR?
from fastify-cors.
Not really understand what to do, what do you mean by "the way we are registering app.js"? @58bits
from fastify-cors.
@58bits thank you, your example works excellent.
from fastify-cors.
@58bits please upgrade to the latest version. I think it was fixed with delvedor/find-my-way#133
from fastify-cors.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from fastify-cors.
Related Issues (20)
- Lack of support with Fastify 4.x HOT 4
- doesnt' work with Typescript HOT 1
- Origin returns undefined HOT 14
- Latency when using a large JWT token HOT 7
- UnhandledPromiseRejectionWarning: AvvioError: Plugin must be a function or a promise HOT 1
- Allow the special `*` value in `array` in `origin` option HOT 3
- Setting for `Cache-Control` to allow preflight requests to be cached in CDNs HOT 1
- Error(s) in doc. HOT 1
- cacheControl default value is missing HOT 1
- Access-Control-Allow-Origin is not working when streaming the data HOT 6
- AvvioError [Error]: Plugin did not start in time: '[object Promise]' HOT 2
- Support Private Network CORS HOT 8
- Options Request Returns 404 running in bun HOT 1
- Missing type declaration file for β@fastify/corsβ module HOT 2
- preflight 404 on prefixed index routes in nested/scopted cors setups
- Multiple @fastify/cors HOT 2
- Do not always define `Vary` header HOT 1
- `preHandler` called twice when rejecting CORS HOT 5
- Vary with dynamic config
- Credentials header is added regardless of origin HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-cors.