How to enable cors nodejs with express?

前端 未结 6 1096
佛祖请我去吃肉
佛祖请我去吃肉 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:23

    To enable cors you can do this:

    var cors = require('cors');
    app.use(cors());
    // to change your ports for different cors stuff:
    app.set('port', process.env.PORT || 3000);
    app.listen(app.get('port'), function() { 
      console.log('we are listening on: ', 
      app.get('port'))
    });
    

    Remember that cors are middleware, so you will want to have app.use before it so that your incoming requests will go through cors before they hit your routes.

    You can change the ports depending on which one you want to use. I am pretty sure you can also replace the || with && to listen on multiple ports and set cors on those.

    In raw node, I believe you have to use the writeHead, but I am not sure about the raw node implementation.

提交回复
热议问题