A
A
Alexander Bondarenko2021-12-07 14:12:47
CORS
Alexander Bondarenko, 2021-12-07 14:12:47

Why doesn't cors express js work?

There is a project on docker with nginx, I set the settings for corsa, and requests go, I tried another ip from another machine, but the request goes

const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.resolve(__dirname, 'static')));


var whitelist = ['https://test.site', 'https://www.test.win']
var corsOptionsDelegate = function (req, callback) {
  var corsOptions;
  if (whitelist.indexOf(req.header('Origin')) !== -1) {
    corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
  } else {
    corsOptions = { origin: false } // disable CORS for this request
  }
  callback(null, corsOptions) // callback expects two parameters: error and options
}

app.options('*', cors(corsOptionsDelegate));

app.use('/api', cors(corsOptionsDelegate) ,router);

app.get('/',cors(corsOptionsDelegate), (req, res) => {
  res.status(200).json({message: 'WORKING!!!'})
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question