ExpressGateway / express-gateway

A microservices API Gateway built on top of Express.js
https://www.express-gateway.io
Apache License 2.0
2.97k stars 344 forks source link

Configuring CORS policy always returns "No Access-Control-Allow-Origin" #994

Closed NomadXD closed 4 years ago

NomadXD commented 4 years ago

I am trying to implement CORS policy with express gateway. My front end runs in localhost:3000 and the back end with the api runs on localhost:8080. I am trying to connect my back end and front end using the following configuration.

policies:
      - cors:
        - action:
            origin: '*'
            credentials: true
            methods: ['GET','POST','PUT','DELETE']
            allowedHeaders: ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Origin', 'Accept', 'X-Requested-With', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Access-Control-Allow-Origin','X-TEST']
            exposedHeaders : ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Origin', 'Accept', 'X-Requested-With', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Access-Control-Allow-Origin','X-TEST']
            preflightContinue: true
      - jwt:
        - action:
            secretOrPublicKey: 'secret'
            checkCredentialExistence: false
      - request-transformer:
        - action:
            body:
              add:
                user: req.user
      - proxy:
        - action:
            serviceEndpoint: noAuthService

When I try to send a request, I get the following error.

Access to XMLHttpRequest at 'http://localhost:8080/api/user/31a19f8a-c76d-4051-a944-dad1b676c550' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Can anyone help me to figure it out ? I tried asking the question in glitter channel but no-one responded. What am I missing here ??

NomadXD commented 4 years ago

@XVincentX Can you please look into this issue and help me out ? Maybe it's just I am missing a simple thing. But nowhere to get help. No example in docs, no gitter and no one answered in stack overflow. Thanks

NomadXD commented 4 years ago

I fixed the issue. It was due to the dummy client code that I used to test the server. I used XMLHttp request to test before and now I switched to node-fetch to create requests from the client. Now it works fine. I think in XMLHttp implementation of pre flight request and response is different. Not sure though. Since my problem is solved, I am closing the issue.