'How to handle CORS with same backend for mobile and web application

I have a basic express js application and i enabled cors to prevent requests from incoming ip

I am simply doing something like this :

const whitelist = [
  'http://localhost:3001',
];

const corsOptions = {
  credentials: true,
  origin: function (origin, callback) {
    console.log('origin', origin);
    if (whitelist.indexOf(origin) !== -1) {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  },
};

app.enableCors(corsOptions);

All works perfectly with my web app but when I am trying to make a request from my mobile app using expo I am getting the error not allowed by cors because the mobile app does not have CORS headers but how can I handle it my CORS logic for both mobile and web app? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source