CORS express not working predictably

前端 未结 6 1028
灰色年华
灰色年华 2021-02-08 08:15

I am trying to allow access from everywhere.

I have tried using app middleware:

app.use(function (req, res, next) {
  res.setHeader(\"Access-Control-Allo         


        
6条回答
  •  爱一瞬间的悲伤
    2021-02-08 08:53

    Following some standard node projects out there, below CORS configuration worked for me always. It requires the npm package 'cors'. Note: Origin * means enabling responses to any origin and replies with status code 200. If this needs to be limited to one domain, update the origin accordingly. Ex: [origin: 'http://exampleui.com']

    var cors = require('cors');
    var corsOptions = {
        origin: '*',
        optionsSuccessStatus: 200,
      }
    app.use(cors(corsOptions));
    app.use(express.json())
    

提交回复
热议问题