You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importCorsfrom'cors';constoriginWhitelist=[/* ... list of valid origins */];// Initializing the cors middlewareconstcors=Cors({methods: ['GET','POST','HEAD'],allowedHeaders: 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Api-Authorize, X-Authorize, Origin',credentials: true,origin: (origin,callback)=>{// NOTE : origin may be undefined for relative URLsif(!origin||originWhitelist.indexOf(origin)!==-1){callback(null,true);}else{callback(newError('Not allowed by CORS'))}},optionsSuccessStatus: 200// some legacy browsers (IE11, various SmartTVs) choke on 204});exportconstapiMiddleware=handler=>async(req,res)=>{awaitcors(req,res,(result)=>{if(resultinstanceofError){reject(result);}else{resolve();}});// ... extend req with utilsreturnhandler(req,res);};
The origin function will receive undefined as request origin, so even if the returned value is true (i.e. accept the request), the resposne will be handled like this :
Sorry, I may have misread your issue. I will need to check it out. Can you please include complete code and complete way to reproduce? I was not sure how to assemble the procided code snipplets into a running app to reproduce your issue. Ideally please provide the following so I can take a look:
Complete server code I can copy and paste and run without modification
Thank you for the consideration. I will have to collect more data myself because I get these reports from users but cannot reproduce myself. However this seems to be the source of the problem; a missing Access-Control-Allow-Origin header :
I will do my best to provide you with either a reproducible example, or a withdrawal of this issue.
For an app requesting data using the relative URL
/api/data
using this method :And an API route handled this way :
With the
apiMiddleware
defined this way :The
origin
function will receiveundefined
as request origin, so even if the returned value istrue
(i.e. accept the request), the resposne will be handled like this :cors/lib/index.js
Lines 58 to 67 in f038e77
The line 62 will actually resolve like this :
Which will not send the
Access-Control-Allow-Origin
header to the client.A fix could be to send
"*"
as default origin :The text was updated successfully, but these errors were encountered: