How to enable cors nodejs with express?

前端 未结 6 1089
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 07:04

In summary I am using a viewer like api of dicom files called cornerstone, for this I connect to the WADO service of dc4chee to get the dicom, dcm4chee runs port 8080, and m

6条回答
  •  独厮守ぢ
    2020-11-29 07:36

    This code is helped me to resolve the resources cors issue with the express. And You can use other options easily with the asynchronous origin configuration.

    var cors = require('cors'); //import cors module
    
    var whitelist = ['http://localhost:8000', 'http://localhost:8080']; //white list consumers
    var corsOptions = {
      origin: function (origin, callback) {
        if (whitelist.indexOf(origin) !== -1) {
          callback(null, true);
        } else {
          callback(null, false);
        }
      },
      methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
      optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
      credentials: true, //Credentials are cookies, authorization headers or TLS client certificates.
      allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'device-remember-token', 'Access-Control-Allow-Origin', 'Origin', 'Accept']
    };
    
    app.use(cors(corsOptions)); //adding cors middleware to the express with above configurations
    

提交回复
热议问题