Skip to content
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

CORS not working #91

Open
wasifali opened this issue Dec 9, 2020 · 2 comments
Open

CORS not working #91

wasifali opened this issue Dec 9, 2020 · 2 comments

Comments

@wasifali
Copy link

wasifali commented Dec 9, 2020

I'm trying to add cors to express app right after setting it,

const app = express();
 
app.use(
    cors({
      origin: ['some_origin', 'some_origin_2'],
    }),
  );

expected behaviour is it should allow requests from 'some_origin' and 'some_origin_2' but instead both are still blocked.

@KaidiMohammed
Copy link

KaidiMohammed commented Dec 9, 2020

Hey,
Try this , cause I think u must pass a function rather than an array

var allowedOrigins = ['http://localhost:3000',
                      'http://yourapp.com'];
app.use(cors({
  origin: function(origin, callback){
    // allow requests with no origin 
    // (like mobile apps or curl requests)
    if(!origin) return callback(null, true);
    if(allowedOrigins.indexOf(origin) === -1){
      var msg = 'The CORS policy for this site does not ' +
                'allow access from the specified Origin.';
      return callback(new Error(msg), false);
    }
    return callback(null, true);
  }
}));

@arvindgemini
Copy link

@KaidiMohammed This is working if we use callback function but it return Access-Control-Allow-Origin: * instead of Access-Control-Allow-Origin: http://domainname.com in request header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants