我使用 Express Gateway 作为我的 API 网关来连接后端服务器。我的前端在 localhost:3000 上运行,Express 网关在 localhost:8080 上运行。当我尝试从前端访问后端时,我的浏览器中出现了 cors 错误。然而,当使用邮递员时,一切都很好。
我的 gateway.config.yml (只有一个管道)
user:
apiEndpoints:
- user
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', 'Content-Type', 'Access-Control-Request-Method', 'Access-Control-Request-Headers','Authorization', 'Access-Control-Allow-Origin','X-TEST']
exposedHeaders : ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Origin', 'Accept', 'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', 'Access-Control-Request-Headers','Authorization', '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
我收到以下错误。
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.
知道如何解决这个问题吗???