2

I'm having an issue with my MEAN stack app on the AWS - Windows. So, I've placed a port 3000 for Node server to listen, and IIS is using port 80 by default.

Then when I start the node server to listen, and fire the app I want to GET some data with Angular through Node. I'm trying with this:

$http.get('http://localhost:3000/get_data').success(function(response) { // something with response });

This reproduce an error in my console:

enter image description here

This is a blocker for me, I would really appreciate if someone can point me to the right way.

I'm also interested to know why this is happening, why I'm getting CORS with port different then default (80)?

Thanks

Wolf87
  • 540
  • 3
  • 11
  • 29

2 Answers2

1

There is a nice node.js package called cors. You can make use of this library in following way

var cors = require('cors')
app.options('/get_data', cors());
app.route('/get_data').get(cors(), doOperation());
1

You can enable Cross origin request at node side by using CORS module. Read more about the module and documentation here: Enable cors

var cors = require('cors');
app.use(cors());
ashfaq.p
  • 5,379
  • 21
  • 35
  • Did you put `app.use(cors())` before all api's ? – ashfaq.p Feb 08 '16 at 08:23
  • I start `app = Express();` and then `app.use(cors());`. I'm also interested to know why this is happening, why I'm getting CORS with different port? – Wolf87 Feb 08 '16 at 08:36
  • http://stackoverflow.com/questions/1077218/are-different-ports-on-the-same-server-considered-cross-domain-ajax-wise –  Feb 08 '16 at 08:38
  • This is because of security reasons. Browsers don't allow cross-origin requests .Read about it here: http://www.html5rocks.com/en/tutorials/cors/ – ashfaq.p Feb 08 '16 at 08:39