Estoy tratando de usar Express Gateway + AXIOS (react) + Express, pero estoy recibiendo el CORS Erro, ya hice muchas cosas pero nada funcionó.
Cross-Origin Resource Sharing error: PreflightMissingAllowOriginHeader
Mi EXPRESS
es así:
const corsOptions = { origin: '*', methods: ['POST', 'GET', 'PATCH', 'DELETE'], allowedHeaders: ['Content-Type', 'Authorization'] } app.use(cors(corsOptions));
Mi EXPRESS-GATEWAY
:
http: port: 8080 admin: port: 9876 host: localhost apiEndpoints: api: host: "localhost" paths: - '/api/B/*' - '/api/A/*' serviceEndpoints: appname: urls: - 'http://localhost:8000' policies: - jwt - cors - expression - log - proxy - rate-limit pipelines: default: apiEndpoints: - api policies: - cors: - action: origin: ["*"] methods: [ "HEAD", "PUT", "PATCH", "POST", "GET", "DELETE" ] credentials: true allowedHeaders: ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Accept','Options','X-Requested-With'] - jwt: - action: secretOrPublicKey: code checkCredentialExistence: false - proxy: - action: serviceEndpoint: appname changeOrigin: true
Mi AXIOS
:
const headers = { headers: { "Authorization": authToken, "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": true }, } axios.get(`${API_PRIVATE_URL}/user/profile`, { crossdomain: true }, { withCredentials: true }, headers)
Ya no sé qué hacer. Alguien me puede ayudar ?
Ya entré en varias publicaciones pero nada funcionó..
editar: tampoco fue al controlador. edit2: Puedo usar con POSTMAN, y funcionó como se esperaba...
Agregue "OPCIONES" después de "ELIMINAR" en sus listas de métodos permitidos en express/express-gateway.
const corsOptions = { origin: '*', methods: ['POST', 'GET', 'PATCH', 'DELETE', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization'] } app.use(cors(corsOptions));
http: port: 8080 admin: port: 9876 host: localhost apiEndpoints: api: host: "localhost" paths: - '/api/B/*' - '/api/A/*' serviceEndpoints: appname: urls: - 'http://localhost:8000' policies: - jwt - cors - expression - log - proxy - rate-limit pipelines: default: apiEndpoints: - api policies: - cors: - action: origin: ["*"] methods: [ "HEAD", "PUT", "PATCH", "POST", "GET", "DELETE", "OPTIONS" ] credentials: true allowedHeaders: ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Accept','Options','X-Requested-With'] - jwt: - action: secretOrPublicKey: code checkCredentialExistence: false - proxy: - action: serviceEndpoint: appname changeOrigin: true