-
Notifications
You must be signed in to change notification settings - Fork 50
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 do we handle cors? #19
Comments
Any takers on this pls? |
var cors = require("cors");
...
var app = expressAppConfig.getApp();
app.use(cors()); Hope this would be helpful. |
Hello, The problem comes from the fact that this plugin configure the app in a bad way. Just to deactivate a type of logs, I had to create a pull request with a simple parameter which deactivate logging. Your problem comes from the configuration of the app in the ExpressAppConfig of the module:
Here you can see that the whole stack of middleware is injected ("app.use") in the app before defining the routes from the open api contract. The problem is that it is really hard to modify the Express middleware stack once you have set the routes. Using app.use(cors()); after you received your app from expressAppConfig, like suggested above without testing it, will be simply ignored because the middleware layers added before by the app configurator will fill some unknown logic before getting to the one added by the cors middleware (probably some header "Access-Control-Allow-Origin" related logic). So, if you modify the module and simply add:
You can now use CORS without problems. In the future, this should be included by default in the module as an option. |
thanks a lot @thomasgainant for your comment. |
I think that the same happens with errorHandler, I haven't override default error format |
my solution for this problem was this:
|
Hi, i just fixed it in my fork https://github.com/slavomir-sidor/oas3-tools you can use it in your index.js 'use strict'; var path = require('path'); var oas3Tools = require('oas3-tools'); var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options); // Initialize the Swagger middleware |
Hi Slavomir, Thanks for the updates. Now If i create another server side nodejs code (for internal) the same directory structure repeats. |
I've found that when you add a middleware to express with app.use it simply add it to the end of the stack (here you can see the app.use function). I've wrote a workaroudn that firstly add the middleware to the stack, then move the "SwaggerRouter" and the "ErrorHandler" at the end of the stack (after the new middleware)
I'm not an expert therefore i'm not sure that works every time, but i've tried with cors and it works. P.S. i've noticed that with the newer version the problem should be fixed, but it is not released in npm |
I still facing with this problem. I can solve it on local with the solutions proposed, but I can't fix it on my remote server. I deploy the API server with docker-compose and I can't make this patch on the docker image. Has someone a solution for this? |
For one's who looking for easy solution: go to generated code response.writeHead(code, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,CONNECT,TRACE"
}); |
Have tried all the suggested solutions in this thread in my code and still running into the same issue. Anyone have an updated suggestion? Edit: Was a bit put off editing the source code as it seems a bit extreme, most other server implementations I've used have an option you can set that will work for this kind of thing. Making the change to the source of the oas3-tools source as suggested by @thomasgainant resolved the issue for me. Thank you. |
This is how I made it
|
Thanks @bouchaala-sabri , this is elegant workaround and it works |
This works with simple requests, but for all others it will fail to answer preflight check. For non-simple requests, browser sends OPTIONS and expects 2xx response, Express responds with 405 Method not allowed, unless you properly initialize CORS middleware... |
Hello,
I am struggling to add CORS support to my app running on swagger server. I tried using swagger-express-middleware but that dint do the trick. Looks like they are not yet supporting openapi 3.0 specs.
Need some pointers please.
I have a middleware code as shown in the example:
My curl call is resulting in a 405 error.
I am sure I am doing something wrong. Appreciate any pointers.
The text was updated successfully, but these errors were encountered: